473,842 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

subprocess wait() waits forever, but os.system returns

There are so many threads on this subject, but I ran across a
situation on Windows that I can't figure out.

I'm trying to run this little command-line exe and when I launch like
this, it hangs:
>>import subprocess
command = r'c:\mydir\foo. exe'
run = subprocess.Pope n(command, shell=True, stdout=subproce ss.PIPE, stdin=None, stderr=subproce ss.PIPE, env=os.environ, universal_newli nes=True)
returncode = run.wait() ## HANGS HERE ##
I can run this exe manually via the command prompt and it returns
after a few seconds, but more importantly when I run it as follows it
works fine:
>>import os
command = r'c:\mydir\foo. exe'
os.system(com mand) ## WORKS FINE! ##
Unfortunately I don't know too much about the exe (well, I do know
that it spits out some stdout that I collect, but I don't know the
exe's source code.)

I can't figure out why the subprocess module is having a hard time
with this particular exe. I've tried so many different permutations
of subprocess.Pope n and they all hang on this exe. Even if try to do
the usual (pseudo code):
while(returncod e is None):
returncode = run.poll()
time.sleep(1)
blah blah blah

returncode is always None... In other words, it's hung. I can't
figure out why os.system works fine, but subprocess.Pope n thinks the
process hasn't finished.

Any ideas would be greatly appreciated. I'm all ears.
Jun 27 '08 #1
4 5868
grayaii schrieb:
There are so many threads on this subject, but I ran across a
situation on Windows that I can't figure out.

I'm trying to run this little command-line exe and when I launch like
this, it hangs:
>>>import subprocess
command = r'c:\mydir\foo. exe'
run = subprocess.Pope n(command, shell=True, stdout=subproce ss.PIPE, stdin=None, stderr=subproce ss.PIPE, env=os.environ, universal_newli nes=True)
returncode = run.wait() ## HANGS HERE ##
The code blocks because you aren't reading from stdout and stderr. Use
the communicate() method instead of wait().

Christian
Jun 27 '08 #2
grayaii schrieb:
There are so many threads on this subject, but I ran across a
situation on Windows that I can't figure out.

I'm trying to run this little command-line exe and when I launch like
this, it hangs:
>>>import subprocess
command = r'c:\mydir\foo. exe'
run = subprocess.Pope n(command, shell=True, stdout=subproce ss.PIPE, stdin=None, stderr=subproce ss.PIPE, env=os.environ, universal_newli nes=True)
returncode = run.wait() ## HANGS HERE ##
The code blocks because you aren't reading from stdout and stderr. Use
the communicate() method instead of wait().

Christian

Jun 27 '08 #3
Awesome! That worked!
And your comment just led me down another exploration path on why the
following doesn't work:
---------------------
while(returncod e is None):
returncode = run.poll()
time.sleep(1)

out = run.stdout.read lines()
err = run.stderr.read lines()
---------------------
Nowhere in the loop am I reading stdout or err.
I'm only reading it after the loop has finished, and when running the
exe, returncode is *always* None.
Now I have to figure out a way to read it within the loop without
blocking the read... on Windows...

Thinking out loud: Perhaps it would be better to put the subprocess
commands in a separate thread so if my process hangs, I can kill it
after a given time. Various threads in this forum suggested that
method, so perhaps I should do it that way...
Jun 27 '08 #4
grayaii schrieb:
Awesome! That worked!
And your comment just led me down another exploration path on why the
following doesn't work:
---------------------
while(returncod e is None):
returncode = run.poll()
time.sleep(1)

out = run.stdout.read lines()
err = run.stderr.read lines()
---------------------
Nowhere in the loop am I reading stdout or err.
I'm only reading it after the loop has finished, and when running the
exe, returncode is *always* None.
Now I have to figure out a way to read it within the loop without
blocking the read... on Windows...
the subprocess has already a canonical way:

out, err = run.communicate ()

Your code may still block if the stderr buffer is full during the
stdout.readline s() call.

Christian

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3409
by: Marc Carter | last post by:
I am trying to rewrite a PERL automation which started a "monitoring" application on many machines, via RSH, and then multiplexed their collective outputs to stdout. In production there are lots of these subprocesses but here is a simplified example what I have so far (python n00b alert!) - SNIP --------- import subprocess,select,sys speakers=
14
3966
by: Ernesto | last post by:
def launchWithoutConsole(command, args): """Launches 'command' windowless and waits until finished""" startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW return subprocess.Popen( + args, startupinfo=startupinfo).wait() handle = launchWithoutConsole("program.exe",) ------------------------------------------------------------------------------------------------------------------------------
2
2487
by: Ernesto | last post by:
I launch a Windows executable and wish to close it from Python. The code is below. Normally, my program waits for rib.exe to finish, but I'd like to be able to close it from python if possible. (I suppose if I was going to do this, I wouldn't use .wait() ) Any ideas ? # Launch a program without launching a whole new cmd prompt def launchWithoutConsole(command, args): """Launches 'command' windowless and waits until finished"""...
12
5281
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock block protecting a resource. Thread 2 and 3 also have a SyncLock block protecting the same resource and after executing some code in their blocks they both do a Monitor.Pulse to hand of the locked resource back to thread 1. While thread 1 has...
9
6500
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This hangs at this point. How should I handle these kind of commands (ping 127.0.0.1) with
4
17552
by: 7stud | last post by:
Hi, What is the difference between: 1) getting the returncode directly from the subprocess object 2) calling poll() on the subprocess object? Here is an example:
12
4545
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's finished, and if it is look at its output. I may want to send a signal at some point to kill the process. This seems straightforward, but it doesn't seem to be working. Here's my test case:
6
275
by: John Mechaniks | last post by:
from subprocess import call call() How do I get the result (not the exit status of the command) of "ls - l" into a variable?
25
2803
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the object itself is a subprocess, not a "popen". It seems that it would be more accurate to just name the class Subprocess, can anyone explain why this is not the case? Thank you.
0
9871
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10610
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10313
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7035
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4089
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3144
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.