Subprocess does not return for longer-running process

Jason Friedman jsf80238 at gmail.com
Wed Sep 22 00:06:00 CEST 2010


Hello,

The Popen call does not return if the underlying OS call runs longish,
as the example below shows.
Although, if the underlying OS call is merely "sleep N" it will return
even after quite a long time.

wikiuser at dvprwiki1:~> python --version
Python 2.6.4

wikiuser at dvprwiki1:~> time /opt/confluence-cli-1.5.0/confluence.sh
--action getPageList --space TIS --user cli --password cli | head -1
132 pages in list

real    0m1.245s
user    0m1.482s
sys     0m0.153s

wikiuser at dvprwiki1:~> time /opt/confluence-cli-1.5.0/confluence.sh
--action getPageList --space Oncall --user cli --password cli | head
-1
19645 pages in list

real    0m14.425s
user    0m9.866s
sys     0m1.110s

wikiuser at dvprwiki1:~> cat $HOME/scripts/subprocess_test.py
#!/usr/bin/env python
import os, sys
from subprocess import Popen, PIPE
def execute(command_list=["sleep", "1"]):
    process = Popen(command_list, shell=False, stdout=PIPE)
    (pid, exit_code) = os.waitpid(process.pid, 0)
    if process.stdout != None:
        print process.stdout.read()
    if process.stderr != None:
        print process.stderr.read()
    return exit_code
if __name__ == "__main__":
    if len(sys.argv) > 1:
        execute(sys.argv[1].split())
    else:
        execute()

wikiuser at dvprwiki1:~> time $HOME/scripts/subprocess_test.py
"/opt/confluence-cli-1.5.0/confluence.sh --action getPageList --space
TIS --user cli --password cli" | head -1
132 pages in list

real    0m1.233s
user    0m1.437s
sys     0m0.169s

wikiuser at dvprwiki1:~> time $HOME/scripts/subprocess_test.py
"/opt/confluence-cli-1.5.0/confluence.sh --action getPageList --space
Oncall --user cli --password cli" | head -1
Traceback (most recent call last):
  File "$HOME/scripts/subprocess_test.py", line 18, in <module>
    execute(sys.argv[1].split())
  File "$HOME/scripts/subprocess_test.py", line 9, in execute
    (pid, exit_code) = os.waitpid(process.pid, 0)
KeyboardInterrupt

real    1m25.306s
user    0m0.021s
sys     0m0.035s

(Note I killed this last command with CNTL-C.)



More information about the Python-list mailing list