Run a process and quit without waiting for it

后端 未结 5 1392
-上瘾入骨i
-上瘾入骨i 2021-02-20 11:36

In Python under Windows: I want to run some code in a separate process. And I don\'t want the parent waiting for it to end. Tried this:

from multiprocessing impo         


        
5条回答
  •  感动是毒
    2021-02-20 12:24

    Use the subprocess module as other subprocess control methods (os.system, os.spawn*, os.popen*, popen2., commands.) are being deprecated:

    from subprocess import Popen
    Popen( [ "foo.exe", "arg1", "arg2", "arg3" )
    

    See the Python doco, especially P_NOWAIT example.

    You will have to start a new Python interpreter in the subprocess, so "foo.exe" above will likely be "python.exe".

    EDIT:

    Having just reviewed the multiprocessing module documentation:

    join_thread(): Join the background thread. This can only be used after close() has been called. It blocks until the background thread exits, ensuring that all data in the buffer has been flushed to the pipe.

    By default if a process is not the creator of the queue then on exit it will attempt to join the queue’s background thread. The process can call cancel_join_thread() to make join_thread() do nothing.

    cancel_join_thread(): Prevent join_thread() from blocking. In particular, this prevents the background thread from being joined automatically when the process exits – see join_thread().

    It looks like you should be able to call cancel_join_thread() to get the behaviour you desire. I've never used this method (and was unaware of it's existence until a minute ago!), so be sure to let us know if it works for you.

提交回复
热议问题