Proper way of re-using and closing a subprocess object

后端 未结 4 757
猫巷女王i
猫巷女王i 2021-01-12 18:56

I have the following code in a loop:

while true:
    # Define shell_command
    p1 = Popen(shell_command, shell=shell_type, stdout=PIPE, stderr=PIPE, preexec         


        
4条回答
  •  自闭症患者
    2021-01-12 19:33

    What is the proper way of closing a subprocess object once we are done using it?

    stdout.close() and stdin.close() will not terminate a process unless it exits itself on end of input or on write errors.

    .terminate() and .kill() both do the job, with kill being a bit more "drastic" on POSIX systems, as SIGKILL is sent, which cannot be ignored by the application. Specific differences are explained in this blog post, for example. On Windows, there's no difference.

    Also, remember to .wait() and to close the pipes after killing a process to avoid zombies and force the freeing of resources.

    A special case that is often encountered are processes which read from STDIN and write their result to STDOUT, closing themselves when EOF is encountered. With these kinds of programs, it's often sensible to use subprocess.communicate:

    >>> p = Popen(["sort"], stdin=PIPE, stdout=PIPE)
    >>> p.communicate("4\n3\n1")
    ('1\n3\n4\n', None)
    >>> p.returncode
    0
    

    This can also be used for programs which print something and exit right after:

    >>> p = Popen(["ls", "/home/niklas/test"], stdin=PIPE, stdout=PIPE)
    >>> p.communicate()
    ('file1\nfile2\n', None)
    >>> p.returncode
    0
    

    Considering the nature of my script, is there a way to open a subprocess object only once and reuse it with different shell commands? Would that be more efficient in any way than opening new subprocess objects each time?

    I don't think the subprocess module supports this and I don't see what resources could be shared here, so I don't think it would give you a significant advantage.

提交回复
热议问题