multithreading - how to run a python file as Thread? -
example python parent file:
class myclass( wx.frame ): def __init__(self): print "prepare execute" self.mythread = thread.runbackground( './child.py' , ( '--username' , 'root' ) ); print "executing... app?" self.mythread.start(); def onclose( self , evt ): self.mythread.close() self.exit(); app = myclass()
i need know how run script in background python. idea main window can used, if second process works.
i'm going take guess here: don't care threads @ all, want "run script" "second process".
that's easy. running script running else; use subprocess
module. since script running inside entirely separate python interpreter instance, in entirely separate process, "the main window can used, if second process works"—or if spins or blocks forever.
for example:
class myclass( wx.frame ): def __init__(self): print "executing... app?" self.child = subprocess.popen([sys.executable, './child.py', '--username', 'root']) def onclose( self , evt ): self.child.wait() self.exit();
the trick here pass first argument. if want make sure child.py
run same copy of python parent, use sys.executable
. if want make sure it's run default python, if parent using different one, use python
. if want use specific one, use absolute path. if want let shell (or, on windows, pylauncher thing) figure out based on #!
line, use shell=true
, pass ./child.py
first argument. , on.
Comments
Post a Comment