Subprocess does not return for longer-running process

J

Jason Friedman

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@dvprwiki1:~> python --version
Python 2.6.4

wikiuser@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@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@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@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@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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top