how to call a program from python without waiting for it to return
Asked Answered
B

3

34

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 when 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.

Brierwood answered 8/4, 2010 at 17:23 Comment(2)
I suggest you shrink the problem down to a small program (should only need a half dozen lines) that demonstrates the problem, and paste that into your question. There are too many possibilities for how things could go wrong for us to know what to say...Marche
possible duplicate of Calling an external command in PythonAudiology
M
5

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.

Marche answered 8/4, 2010 at 17:48 Comment(4)
tried doing this, but it still doesn't work. tried it with Popen and os.system.Brierwood
Well, start does work, and should (I believe) how I described, so without either sample code or a description of in what way it doesn't work, I can't help more. I mean, does it launch your program but your Python app can't exit? Or you can't even get it to start your program? What arguments did you pass it?Marche
start does work. but using it with python doesn't, somehow. edited the question, but just to clarify, the python program is: <p>os.system("start test.exe") <p>print "Done" <p>it starts test.exe, and Done is printed. but the script doesn't stop execution, it seems the process is still tied to test.exe.Brierwood
It must be something special in the nature of your test.exe program. I've tried this in various ways here, and START always does precisely what you say you want... unfortunately I guess you'd have to use my computer to get the same results. ;-)Marche
C
4

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

Cypress answered 23/9, 2022 at 8:52 Comment(1)
works with ubuntu 22Tympanum
L
3

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()
Laster answered 8/4, 2010 at 17:27 Comment(3)
thanks. i have tried using subprocess, but when i set shell=True on Popen, it runs perfectly under eclipse+pydev but won't run when built as an executable on py2exe. i'm guessing there is a conflict with my py2exe built with 32-bit python and the actual command being called built as 64-bit. anyways, i can maybe work around that. the script still isn't quitting, though.Brierwood
@maranas: in that case you could simply create a new thread and spawn the process from there. Set thread.daemon to True and it won't stall your process on exit :)Laster
thanks again for the quick reply. it should work, but somehow the main script is not terminating. it just executes to completion but it does not terminateBrierwood

© 2022 - 2024 — McMap. All rights reserved.