programmatically executing and terminating a long-running batch process in python

后端 未结 3 2067
说谎
说谎 2021-01-06 03:04

I have been searching for a way to start and terminate a long-running \"batch jobs\" in python. Right now I\'m using \"os.system()\" to launch a long-running batch job insid

相关标签:
3条回答
  • 2021-01-06 03:14

    If you want control over start and stop of child processes you have to use threading. In that case, look no further than Python's threading module.

    0 讨论(0)
  • 2021-01-06 03:27

    If you are on a Posix-compatible system (e.g., Linux or OS X) and no Python code has to be run after the child process, use os.execv. In general, avoid os.system and use the subprocess module instead.

    0 讨论(0)
  • 2021-01-06 03:28

    subprocess module is the proper way to spawn and control processes in Python.

    from the docs:

    The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several other, older modules and functions, such as:

    os.system
    os.spawn
    os.popen
    popen2
    commands

    so... if you are on Python 2.4+, subprocess is the replacement for os.system

    for stopping processes, check out the terminate() and communicate() methods of Popen objects.

    0 讨论(0)
提交回复
热议问题