is there a way to call a program from python without waiting for it to return? i created a script which copies a program to a directory and runs that program. but w开发者_JAVA百科hen i call the program from python, the python script does not exit until the program i launched exits. i have tried os.system and Popen. is there another way to do this?
Added info: os.spawnl with os.P_DETACH still doesn't work; according to the docs, "P_DETACH is similar to P_NOWAIT, but the new process is detached from the console of the calling process". but it is still somehow attached to my calling process (calling script won't quit until any of the called executables return)
Program:
os.system("start test.exe")
print "Done"
after it executes test.exe, it prints Done. but it does not terminate the script's execution (script process still running). tried creating a daemon thread and Popen with a P_DETACH, still no go.
Under Windows, if you invoke the program using the shell START
command you should be able to "release" the parent process and allow it to exit. Try START /?
at the DOS prompt to learn more.
By using poll()
instead of wait()
on Popen
it will not block and it won't wait for the program to run. However, I think the only way to really stop the entire program from waiting is by creating a daemonic thread which starts the process. That way you'll never have to wait for it.
class MyThread(threading.Thread):
def run(self):
'''Start your thread here'''
pass
thread = MyThread()
thread.daemon = True
thread.start()
I had this same question, but I didn't find the answers listed here to work. After managing to study the documentation: https://docs.python.org/3/library/os.html#os.spawnv I found a solution
import os
filepath = "c:\\a\\b.exe"
process_id = os.spawnv(os.P_NOWAIT , filepath , ["-someFlag" , "someOtherFlag"])
print(process_id)
That launched my excutable, with command line arguments, and did not wait for the process to exit, just to be created. This was on windows with python 3.10
精彩评论