How to start a background process in Python?

后端 未结 7 573
庸人自扰
庸人自扰 2020-11-22 02:44

I\'m trying to port a shell script to the much more readable python version. The original shell script starts several processes (utilities, monitors, etc.) in the background

7条回答
  •  不思量自难忘°
    2020-11-22 03:00

    Note: This answer is less current than it was when posted in 2009. Using the subprocess module shown in other answers is now recommended in the docs

    (Note that the subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using these functions.)


    If you want your process to start in the background you can either use system() and call it in the same way your shell script did, or you can spawn it:

    import os
    os.spawnl(os.P_DETACH, 'some_long_running_command')
    

    (or, alternatively, you may try the less portable os.P_NOWAIT flag).

    See the documentation here.

提交回复
热议问题