python call external program non-blocking way

后端 未结 2 801
刺人心
刺人心 2020-12-10 19:19

is there a way to call an external program inside python and don\'t wait for its execution to finish?

I tried this, but no luck:

os.system(\"external         


        
相关标签:
2条回答
  • 2020-12-10 20:03

    Forget about os.system(). It is deprecated in favour of the subprocess module.

    It provides a way to execute subprograms for almost every thinkable use case.

    0 讨论(0)
  • 2020-12-10 20:05

    Yes, use the subprocess module. For example:

    p = subprocess.Popen(['external_program', 'arg1', 'arg2'])
    # Process is now running in the background, do other stuff...
    ...
    # Check if process has completed
    if p.poll() is not None:
        ...
    ...
    # Wait for process to complete
    p.wait()
    
    0 讨论(0)
提交回复
热议问题