简体   繁体   中英

Popen reading from stdout takes a very very long time

I am trying to capture the output from a shell command (npm --version) however only the first line is read and the process does not end.

import subprocess
proc = subprocess.Popen(['npm', '--version'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
proc.wait()
for line in proc.stdout:
    print(line.decode("utf-8").strip())
print("does not get here?!")

Any idea how I could detect the end of this process?.

If I open a cmd and execute 'npm --version', it ends as expected so I do not know why this done in the above does not end.

Some extra information that maybe of use....

  • npm is installed via nvm
  • this is used to manage node installs via symlinks
  • npm from what I can see is a.cmd file that executes node?

Running this in python command prompt...

>>> import subprocess
>>> proc = subprocess.Popen(['npm', '--version'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
>>> proc.wait()
0
>>> proc.stdout.readline()
'6.10.3\n'
>>> proc.stdout.readline()
''

Now the second.readline() takes a very very long time to complete!

Using stdout=PIPE and/or stderr=PIPE in popen.wait() will cause a deadlock. Try using communicate() to avoid that. This is due to other OS pipe buffers filling up and blocking the child process. See this documentation on how to use communicate () https://docs.python.org/2/library/subprocess.html

Hope I could help!

Can you please share the console output when you manually type on the command prompt please.

The code you have shared works on my machine and i am assuming it may have to do something with the way npm is installed. In any case can you share the output from command console.

Thanks Pushpa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM