473,504 Members | 13,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

popen2.Popen4: "wait()" hangs on

Hello,

I use popen2.Popen4 and I experienced problems with it. Let me show you with
an example. The following script is called "lines" (it prints lines of
'X'):

---------------------
#!/usr/bin/env python

import sys

for line in range(0, int(sys.argv[1])):
print "X" * 120
---------------------

In a Python console, I launch it:

---------------------
Python 2.3.4 (#2, Jul 5 2004, 09:15:05)
[GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2
import popen2
f = popen2.Popen4("lines 50000")
f.wait() ---------------------

"f.wait()" doesn't return anything. It just hangs on. "f.poll()" returns -1
all the time, even if "lines 50000" should end within one or two seconds.

My guess is that too many characters are sent to the standard output. I
found a way to deal with this problem:

---------------------
Python 2.3.4 (#2, Jul 5 2004, 09:15:05)
[GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2 import popen2
f = popen2.Popen4("lines 50000")
lines = f.fromchild.readlines()
f.wait()

0
---------------------

It works! But it doesn't work in all cases. It happened to me that
"f.fromchild.readlines()" returns lines before the end of process:
"f.wait()" is then called before the end of the process and it may hang
on...

Finally, I usually write:
---------------------
lines = []
while (f.poll() == -1):
lines += f.fromchild.readlines()
---------------------
which seems to work in any case.

Is my work-around correct? Is there another one?
And why doesn't "wait()" work well? I suspect problems with full buffers or
unflushed buffers, but I am not sure.

Thank you for your help,
Vivien.
Jul 18 '05 #1
1 2645
On Mon, 26 Jul 2004, Vivien Mallet wrote:
"f.wait()" doesn't return anything. It just hangs on. "f.poll()" returns -1
all the time, even if "lines 50000" should end within one or two seconds.

My guess is that too many characters are sent to the standard output. I
found a way to deal with this problem:


Your diagnosis is nearly correct. The actual reason is that "lines" is
trying to write output, but since nothing is consuming the output, it is
forced to block on its 'print' statements. Because f.wait() is waiting
for "lines" to finish, it will keep blocking until "lines" is able to
write its output somewhere.

The solutions you give are quite correct; they consume the output before
waiting for the process to complete (I might also suggest using select()
in place of .poll(), though). What is it you're actually trying to
accomplish, though? If you're just trying to run a process in parallel,
but ignore its output, one of the spawn() family of calls might work
better (these will send the output to the same place as your main
program). If you want to discard the output entirely, then you can use a
combination of fork() and one of the exec() calls, redirecting sys.stdout
to /dev/null in the process.

Hope this helps.

Jul 18 '05 #2

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

Similar topics

3
13455
by: Kannan | last post by:
Hello, I have a requirement that specifies that I display a "Please Wait..." window (preferably modal) along with a gif that mimics a progress bar during a save operation. The dialog needs to be...
1
2127
by: Alex Li | last post by:
Dear js/xmlhttp experts, I spent hours but could not solve this problem and hope someone could give me a clue: a onclick event will invoke a function to do a few things: 1. make a hidden DIV...
1
1011
by: metsymani | last post by:
In my web application, I have a search screen coded in ASP.Net. The Search process takes lot of time. So, I need to show a wait page informing the user that "Search is in progress. Please wait" along...
3
4611
by: John Dalberg | last post by:
Hi I have a form that opens a new window for the results. Because the results might take a few seconds due to server processing, I would like to display a message "please wait" in the new...
1
2437
by: Huey | last post by:
Hi All, I encountered a funny thing, and my code schetch as below: #define READ 0 #define WRITE 1 int byteRead, status, pd; char buff;
3
4110
by: Aaron Queenan | last post by:
I have a form which performs some asynchronous code. I want to display a wait cursor when it starts, and hide the wait cursor when it has completed. This part works fairly well, using: ...
1
4549
by: VMI | last post by:
How can I add a small "Please wait..." form to a child form so that when I minimize the child form, the "Please Wait..." form will also disappear? This form will be displayed when the child form is...
5
3222
by: Jeremy | last post by:
Hi all, I have database actions that will potentially take several seconds to complete. My normal page uses AJAX so keeping the user informed of what is happening is not a problem. ...
1
2351
by: =?Utf-8?B?Sm9obiBXYWxrZXI=?= | last post by:
Hi, I have a webpage designed with asp.net 2.0. Is there a way to display a "please wait" message to the screen horizontally centered and veritcally 20px from the VISIBLE top of the page,...
0
7098
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...
0
7366
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...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
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...
0
5610
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,...
0
4698
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...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
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 ...

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.