473,657 Members | 2,378 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.rea dlines()
f.wait()

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

It works! But it doesn't work in all cases. It happened to me that
"f.fromchild.re adlines()" 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.rea dlines()
---------------------
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 2655
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
13469
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 closed at the end of the operation. How do I go about this... Issues are: 1. I believe a showModaldialog would stop all back ground processing and shift control to the modal dialog. 2. I would like to close the dialog from the parent window...
1
2134
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 "wait" message visible by changing its style.display to "block" 2. download the data with XMLHTTP 3. make the hidden DIV message invisible by setting its display to "none".
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 with an animated gif. I have placed a div in the search page which contains the message and animated gif. I hide the div by making "display: none" in the onload function of the body tag. On click of the Search button(type = Submit), I call a...
3
4626
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 window, erase that message when the processing is done and show the results. How is this done? John Dalberg
1
2446
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
4120
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: this.TopLevelControl.Cursor = System.Windows.Forms.Cursors.WaitCursor; I also want to prevent user interaction with the controls on my form while the wait cursor is displayed, so that use events (except resize and cancel) are blocked. What is the...
1
4565
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 running a lengthy process or when the child form is displayed. Once the child form finishes the process, or it's minimized, the "wait" form will also disappear. Thanks.
5
3231
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. However, the page also has fallback code in case of no Javascript. In this case I would like to display temporary "please wait" type page
1
2364
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, regardless of what kind of scrolling the user has done? I am currently displaying a "please wait" message (the DIV section shown below, which i'm showing/hiding) when the user clicks save, but the user is
0
8399
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
8312
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
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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
8606
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
7337
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
4159
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2732
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
1622
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.