Pass variable script return twice output problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Pass variable script return twice output problem (/thread-14946.html) |
Pass variable script return twice output problem - Faruk - Dec-26-2018 Hello guys. I write two python code and I can pass variable two scrpit but I have some problem. My scrpits this: script1.py x='a' import script2script2.py import script1 import time print "second script start" print script1.x print "secon script end"When I ran script2.py result this: Why script2.py return result twice. How can i fix twice run problem? Thanks
RE: Pass variable script return twice output problem - Larz60+ - Dec-26-2018 remove 'import script2' from script1 RE: Pass variable script return twice output problem - Faruk - Dec-26-2018 Thanks for your quike reply. Good works. RE: Pass variable script return twice output problem - Gribouillis - Dec-26-2018 When you run script2 as the main program, python meets import script1 at line 1. Python creates a new empty module object and stores it in sys.modules['script1'] . It then run's script1's code in this module's global namespace. There it meets import script2 . As there is still nothing in sys.modules['script2'] , python creates a new module object here and starts running script2.py's code in this module's global namespace. There,it meets import script1 again! This time, it does nothing because there is already a module in sys.modules['script1'] , so it proceeds with script2, printing the three lines once. After this we've only executed the import scrip2 from script1, which was called because of the import script1 from the main program. It remains to run the rest of the main program (script2.py), so that python runs the prints a second time!The first fix is to separate things that you want to print only when script2 is the main program but not when it is imported import script1 import time if __name__ == '__main__': print "second script start" print script1.x print "secon script end"The second fix is to avoid circular imports by all the means. It can always been done. In your case, why would you need to import script2 into script1? Last but not least: use python 3! RE: Pass variable script return twice output problem - Faruk - Dec-26-2018 I have a project on Rpi with python and I want check status of my main program with other script. If I don't take any output I should reboot my rpi.(as software WDT) So I work for pass variable between two script. RE: Pass variable script return twice output problem - Gribouillis - Dec-26-2018 Faruk Wrote:So I work for pass variable between two script.If you need to share variables between the two scripts and these variables cannot go in one of the scripts, use a third module # script3.py shared_list = [] # script1.py import script3 script3.shared_list.append('Spam') # script2.py import script3 print(script3.shared_list) RE: Pass variable script return twice output problem - Faruk - Dec-26-2018 Thanks. I will try and share result. It is work fine but there is one problem. I want check GUI prgoram and if I does not close GUI window, I can not see any output. When I close GUI window I see all output incoming. So I can not check my main program real time. How can I fix this? RE: Pass variable script return twice output problem - Gribouillis - Dec-26-2018 You need to post more code because without the code it is impossible to debug. RE: Pass variable script return twice output problem - Faruk - Dec-26-2018 I think first script runs second script and first script wait while second script finish its process. After second script finish its process, first script continue work. Ok. I hare my code PGUI.py #! /usr/bin/python # -*-coding: utf-8 -*- #-Library setting-# import RPi.GPIO as gpio import time from timeit import default_timer as timer import serial from Tkinter import Tk, Label from PIL import ImageTk, Image import threading import subprocess import pigpio import os import signal pi=pigpio.pi() print (pi.connected) gpio.setmode(gpio.BCM) gpio.setwarnings(False) gpio.setup(33, gpio.IN) gpio.setup(30, gpio.OUT) gpio.setup(39, gpio.OUT) ##pwm=gpio.PWM(30,100) ##pwm.start(0) #-Buffer definition-# dataarray='' data='' uartData='' shadowdata=[] dstflg=False dspflg=False dataflg=False testflg=False floorinfoflg=False screenflg=False soundflg=False doorflg=False control_count=0 #-Serial port config-# try: stmSer=serial.Serial(port='/dev/ttyAMA0', baudrate=115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=0.01 ) except serial.SerialException: import traceback traceback.print_exc() print "Stmser except error" def serialflush(): stmSer.flushInput() stmSer.reset_input_buffer() serialflush() def screenopen(): global screenflg ## screenflg=False print "screen open start" gpio.setwarnings(False) gpio.output(39,1) pi.set_PWM_dutycycle(30,255) try: for i in range(0,155,30): pi.set_PWM_frequency(30,i) time.sleep(0.01) print "screen open end" except: import traceback traceback.print_exc() prgloop() def screenclose(): global screenflg ## screenflg=True print "screenclose start" gpio.setwarnings(False) gpio.output(39,0) pi.set_PWM_frequency(30,255) try: for i in range(155,0,-5): pi.set_PWM_dutycycle(30,i) time.sleep(0.03) print "screenclose end" except: import traceback traceback.print_exc() prgloop() #-GUI update according to data of 'CARSTAT'-# def updateGUI(data): global left_character global right_character global call_up global call_down global travel_up global travel_down global chime global floorinfoflg global control global shadowdata global car_light global screenflg global soundflg global doorflg try: left_character=(data[10:12]).decode("hex") #print "left_character:"+left_character right_character=(data[12:14]).decode("hex") #print"right_character:"+right_character petra_status_1=format(ord(data[14:16].decode("hex")),'08b') #print "status:"+petra_status_1 petra_status_2=format(ord(data[16:18].decode("hex")),'08b') # print "UP:"+travel_up # print "DOWN:"+travel_down ## katno.config(text=left_character+right_character, font=("Arial", 300)) #print "UpdaaeGUI work" screencl=threading.Thread(target=screenclose) screenop=threading.Thread(target=screenopen) ## sound=threading.Thread(target=Announce) if shadowdata!=data: chime=petra_status_1[7] travel_down=petra_status_1[4] travel_up=petra_status_1[3] car_light=petra_status_1[2] doorAopen=petra_status_2[3] doorAclose=petra_status_2[4] doorBopen=petra_status_2[5] doorBclose=petra_status_2[6] ## print "chime"+chime ## print "UP:"+travel_up ## print "DOWN:"+travel_down ## ## print "doorAOP:"+doorAopen ## print "doorACL:"+doorAclose ## ## print "doorBOP:"+doorBopen ## print "doorBCL:"+doorBclose ## if chime=='1': ## soundflg=True ## print "soundflg True" ## ## if doorAopen=='1' and soundflg==True: ## if chime=='1': ## doorflg=False ## else: ## doorflg=True ## soundflg=False ## print "doorflg True" ## time.sleep(0.1) ## sound.start() if car_light=='0' and screenflg==False : #screenclose() screenflg=True screencl.start() elif car_light=='1' and screenflg==True: #screenopen() screenflg=False screenop.start() #print "carlight"+car_light if travel_up=="1": image=Image.open("/home/pi/Petra/upArrow2.png") photo=ImageTk.PhotoImage(image) label.config(image=photo) label.photo_ref=photo if left_character=="1" or left_character=="2": katno.config(text=left_character+right_character, font=("Arial", 210)) katno.place(x=0,y=40) else: katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-90,y=-30) elif travel_down=="1": image=Image.open("/home/pi/Petra/downArrow2.png") photo=ImageTk.PhotoImage(image) label.config(image=photo) label.photo_ref=photo if left_character=="1" or left_character=="2": katno.config(text=left_character+right_character, font=("Arial", 210)) katno.place(x=0,y=40) else: katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-90,y=-30) elif travel_up=="0" and travel_down=="0": image=Image.open("/home/pi/Petra/upArrow2.png") photo=ImageTk.PhotoImage(image) label.config(image='') label.photo_ref=photo if left_character=="0": katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-150, y=-30) elif left_character=="1" or left_character=="2": katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-150, y=-30) else: katno.config(text=left_character+right_character, font=("Arial", 300)) katno.place(x=-190, y=-30) #time.sleep(0.01) else: print "travel conditions ERROR" else: time.sleep(0.01) #print "control GUI" dataarray='' root.after(20,loop) except: import traceback traceback.print_exc() print "UpdateGUI Error" prgloop() #-Announce-# ##def Announce(): ## global soundflg ## global right_character ## global left_character ## global doorflg ## ## """Kapı kapanma ile birlikte anons aktif hale getirildi ##seslerin üstüste binmemesi için bekleme zamanı koyuldu.""" ## print "Announce start" ## try: ## if left_character=="0": ## soundproc=subprocess.Popen(['omxplayer', '/home/pi/Petra/Announced/Entrance.wav'],stdin=subprocess.PIPE, shell=False) ## elif right_character=="1" and doorflg==True: ## soundproc=subprocess.Popen(['omxplayer', '/home/pi/Petra/Announced/floor1.wav'],stdin=subprocess.PIPE, shell=False) ## elif right_character=="2" and doorflg==True: ## soundproc=subprocess.Popen(['omxplayer', '/home/pi/Petra/Announced/floor2.wav'],stdin=subprocess.PIPE, shell=False) ## elif right_character=="3" and doorflg==True: ## soundproc=subprocess.Popen(['omxplayer', '/home/pi/Petra/Announced/floor3.wav'],stdin=subprocess.PIPE, shell=False) ## elif right_character=="4" and doorflg==True: ## soundproc=subprocess.Popen(['omxplayer', '/home/pi/Petra/Announced/floor4.wav'],stdin=subprocess.PIPE, shell=False) ## elif right_character=="5" and doorflg==True: ## soundproc=subprocess.Popen(['omxplayer', '/home/pi/Petra/Announced/floor5.wav'],stdin=subprocess.PIPE, shell=False) ## elif right_character=="6" and doorflg==True: ## soundproc=subprocess.Popen(['omxplayer', '/home/pi/Petra/Announced/floor6.wav'],stdin=subprocess.PIPE, shell=False) ## elif right_character=="7" and doorflg==True: ## soundproc=subprocess.Popen(['omxplayer', '/home/pi/Petra/Announced/floor7.wav'],stdin=subprocess.PIPE, shell=False) ## else: ## time.sleep(1) ## ## time.sleep(4) ## doorflg=False ## ## print "Announce stop" #### if soundproc.poll() is None: #### try: #### soundproc.stdin.write('q') #### except: #### pass #### try: #### soundproc.terminate() #### except: #### pass ## ## ## except: ## import traceback ## traceback.print_exc() ## print "Announce Error" ## prgloop() #-Reset-# def prgloop(): print "reset loop" testflg=False root.after(20,loop) ###-Data Read -# def readuart(): global dataread global datalist global dataarray global dstflg global dspflg global testflg global stmSer while True: try: if stmSer.inWaiting()>0: dataread=stmSer.read(1) if testflg==True: dstflg=True dspflg=True if dataread=='@' and dstflg==False: dstflg=True dspflg=False dataarray='' elif dataread=='$' and dspflg==False: dspflg=True dstflg=False #print "dataarray: "+dataarray return dataarray import contr else: if dstflg==True and dspflg==False: dataarray=dataarray+''.join(dataread) elif dstflg==True and dspflg==True: print "Reset loop" dataarray='' uartData='' testflg=False dstflg=False dspflg=False root.after(50,readuart) elif dstflg==False and dspflg==False: print "Reset loop" dataarray='' uartData='' testflg=False dstflg=False dspflg=False root.after(50,readuart) else: print "ERROR!!" input_value = gpio.input(33) ##Button Pgm_DW if input_value == True: testflg=True dataarray='' uartData='' print('The button has been pressed...') else: time.sleep(0.02) #data gelmediği zaman program burada uykuya girmeli #print "Data bekleniyor." except: import traceback traceback.print_exc() print "readUart Error" prgloop() #-Furcation to update function -# def loop(): global dataread global datalist global dataarray global dstflg global uartData global dspflg global data global start_time global end_time start_time=time.time() dspflg=False try: uartData=readuart() if uartData[:8]=='CARSTAT,': updateGUI(uartData) #print "data: "+uartData end_time=time.time() ## print end_time-start_time else: #print "İnvalid Data" root.after(20,loop) except: print "Update ERROR" import traceback traceback.print_exc() prgloop() screenOpBoot=threading.Thread(target=screenopen()) screenOpBoot.start() #-Graphical Design-# try: root=Tk() root.title('V0.8') root.overrideredirect(0) katno=Label(root, text='1', font=("Arial", 300), width=2, fg='white', bg='#2080FF') katno.place(x=-90, y=-40) katno.config(padx=170) image=Image.open('/home/pi/Petra/upArrow2.png') photo=ImageTk.PhotoImage(image) label=Label(image=photo, bg="#2080FF") label.image=photo label.place(x=30, y=50) image2=Image.open('/home/pi/Petra/image_new.jpg') photo2=ImageTk.PhotoImage(image2) label2=Label(image=photo2, bg="#142F5E") label2.image=photo2 label2.config(height=375, width=480) label2.place(x=0, y=430) root.configure(background="#2080FF") root.geometry('480x400+0+0') root.after(30,loop) root.mainloop() except: import traceback traceback.print_exc() L5=Label(root, text=(traceback.print_exc())) L5.place(x=10, y=40) print "GUI ERROR"reset.py #!/usr/bin/python from watchdog import Watchdog import os import time import PGUI ##def function(): ## print PGUI.start_time ## print PGUI.end_time ## print "reset function end " def wait(): time.sleep(0.01) go() def go(): for i in range(0,50,1): print PGUI.dataarray time.sleep(0.01) wait() try: with Watchdog(1): go() except Watchdog: print "function took too long to complete" ## os.system('sudo reboot')script1.py import PGUI PGUI.dataarray.append('spam')My main program is PGUI. PGUI work with uart and it show incoming data on GUI. "reset" program is check for PGUI run or not. If PGUI does not work, "reset" program will reboot Rpi. script1.py is your suggestion, I try. So data of PGUI should work on "reset" program same time. Any freeze in GUI should start "reset.py" |