Python : Creating A New Process
I am new to Python . I was supposed to create a GUI with multiple menus . On clicking a particular menu a new process should start and it should not hang the User Interface . But i
Solution 1:
import threading
classsimpleapp_tk(Tkinter.Tk):
# .............................defOnButtonClick(self):
thr = threading.Thread(target=self.print_deep)
thr.start()
defprint_deep(self):
whileTrue:
print'deep'
If you want to create a new process you need to create separate script that will run in separate process. I still thing that better solution will be using threads.
proc.py
whileTrue:
print('deep')
parent_proc.py
import subprocess
import sys
classsimpleapp_tk(Tkinter.Tk):
# .............................defOnButtonClick(self):
subprocess.Popen(args=['python', 'proc.py'], stdout=sys.stdout])
Solution 2:
You don't need multiple threads to start an external process. Popen()
doesn't wait for the child process to end. It returns immediately. Here's a complete code example that starts/stops a tkinter progressbar on the start/end of a subprocess without using threads
.
Unrelated: to run while True: print 'deep'; time.sleep(1)
without blocking the GUI (assuming print('deep')
does not block for long), you could use parent.after()
method:
defOnButtonClick(self):
self._step()
def_step(self):
print('deep')
self.parent.after(1000, self._step) # call _step in a second
Post a Comment for "Python : Creating A New Process"