This issue has been raised a couple of times I am sure. But I have yet
to find a satisfying answer.
I am reading from a subprocess and this subprocess sometimes hang, in
which case a call to read() call will block indefinite, keeping me from
killing it.
The folloing sample code illustrates the problem:
proc = subprocess.Pope n(['/usr/bin/foo', '/path/to/some/file'],
stdout=subproce ss.PIPE)
output = StringIO.String IO()
while True:
r = select.select([proc.stdout.fil eno()], [], [], 5)[0]
if r:
# NOTE: This will block since it reads until EOF
data = proc.stdout.rea d()
if not data:
break # EOF from process has been reached
else:
output.write(da ta)
else:
os.kill(proc.pi d, signal.SIGKILL)
proc.wait()
<Process the output...>
As the NOTE: comment above suggests the call to read() will block here.
I see two solutions:
1. Read one byte at a time, meaning call read(1).
2. Read non-blocking.
I think reading one byte at a time is a waste of CPU, but I cannot find
a way to read non-blocking.
Is there a way to read non-blocking? Or maybe event a better way in
generel to handle this situation?
Thanks
Thomas 5 12869
In message <ma************ *************** ***********@pyt hon.org>, Thomas
Christensen wrote:
r = select.select([proc.stdout.fil eno()], [], [], 5)[0]
if r:
# NOTE: This will block since it reads until EOF
data = proc.stdout.rea d()
No, it will read what data is available.
In message <gf**********@l ust.ihug.co.nz> , Lawrence D'Oliveiro wrote:
In message <ma************ *************** ***********@pyt hon.org>, Thomas
Christensen wrote:
> r = select.select([proc.stdout.fil eno()], [], [], 5)[0] if r: # NOTE: This will block since it reads until EOF data = proc.stdout.rea d()
No, it will read what data is available.
Sorry, maybe not. But you can set O_NOBLOCK on the fd.
On Nov 7, 6:54*am, Thomas Christensen <thom...@thomas christensen.org >
wrote:
This issue has been raised a couple of times I am sure. *But I have yet
to find a satisfying answer.
I am reading from a subprocess and this subprocess sometimes hang, in
which case a call to read() call will block indefinite, keeping me from
killing it.
The folloing sample code illustrates the problem:
* proc = subprocess.Pope n(['/usr/bin/foo', '/path/to/some/file'],
* * * * * * * * * * * * * stdout=subproce ss.PIPE)
* output = StringIO.String IO()
* while True:
* * * r = select.select([proc.stdout.fil eno()], [], [], 5)[0]
* * * if r:
* * * * * # NOTE: This will block since it reads until EOF
* * * * * data = proc.stdout.rea d()
* * * * * if not data:
* * * * * * * break *# EOF from process has been reached
* * * * * else:
* * * * * * * output.write(da ta)
* * * else:
* * * * * os.kill(proc.pi d, signal.SIGKILL)
* proc.wait()
* <Process the output...>
As the NOTE: comment above suggests the call to read() will block here.
I see two solutions:
1. Read one byte at a time, meaning call read(1).
2. Read non-blocking.
I think reading one byte at a time is a waste of CPU, but I cannot find
a way to read non-blocking.
Is there a way to read non-blocking? *Or maybe event a better way in
generel to handle this situation?
Thanks
* * * * * * * * Thomas
As far as I know, you can use '''fctnl''' to make a file handle non-
blocking.
But :
1. I don't know if it works on standard io
2. If it works in python
On Nov 6, 2:54*pm, Thomas Christensen <thom...@thomas christensen.org >
wrote:
This issue has been raised a couple of times I am sure. *But I have yet
to find a satisfying answer.
I am reading from a subprocess and this subprocess sometimes hang, in
which case a call to read() call will block indefinite, keeping me from
killing it.
The folloing sample code illustrates the problem:
* proc = subprocess.Pope n(['/usr/bin/foo', '/path/to/some/file'],
* * * * * * * * * * * * * stdout=subproce ss.PIPE)
* output = StringIO.String IO()
* while True:
* * * r = select.select([proc.stdout.fil eno()], [], [], 5)[0]
* * * if r:
* * * * * # NOTE: This will block since it reads until EOF
* * * * * data = proc.stdout.rea d()
* * * * * if not data:
* * * * * * * break *# EOF from process has been reached
* * * * * else:
* * * * * * * output.write(da ta)
* * * else:
* * * * * os.kill(proc.pi d, signal.SIGKILL)
* proc.wait()
* <Process the output...>
As the NOTE: comment above suggests the call to read() will block here.
I see two solutions:
1. Read one byte at a time, meaning call read(1).
2. Read non-blocking.
I think reading one byte at a time is a waste of CPU, but I cannot find
a way to read non-blocking.
Is there a way to read non-blocking? *Or maybe event a better way in
generel to handle this situation?
From what I understand, you want a way to abort waiting on a blocking
read if the process is hung.
There are some challenges about how you decide if the process is hung
or just busy doing work without generating output for a while (or may
be the system is busy and the process didn't get enough CPU due to
other CPU hungry processes).
Assuming you have a valid way to figure this out, one option is to
have a timeout on the read.
If the timeout exceeds, you abort the read call. No, the read doesn't
provide a timeout, you can build one using alarm.
def alarm_handler(* args):
""" This signal stuff may not work in non unix env """
raise Exception("time out")
signal.signal(s ignal.SIGALRM, alarm_handler)
try:
signal.alarm(ti meout) # say timeout=60 for a max wait of 1
minute
data = proc.stdout.rea d()
except Exception, e:
if not str(e) == 'timeout': # something else went wrong ..
raise
# got the timeout exception from alarm .. proc is hung; kill it
Karthik
>
Thanks
* * * * * * * * Thomas
On Nov 7, 9:09*am, Lawrence D'Oliveiro <l...@geek-
central.gen.new _zealandwrote:
In message <gf07sh$in...@l ust.ihug.co.nz> , Lawrence D'Oliveiro wrote:
In message <mailman.3600.1 226012406.3487. python-l...@python.org >, Thomas
Christensen wrote:
* * * r = select.select([proc.stdout.fil eno()], [], [], 5)[0]
* * * if r:
* * * * * # NOTE: This will block since it reads until EOF
* * * * * data = proc.stdout.rea d()
No, it will read what data is available.
Sorry, maybe not. But you can set O_NOBLOCK on the fd.
Set O_NONBLOCK on proc.fileno() and try using os.read() on that
descriptor.
-srp This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Markus Breuer |
last post by:
I have a question about oracle commit and transactions. Following scenario:
Process A performs a single sql-INSERT into a table and commits the
transaction. Then he informs process B (ipc) to read the new date. So
process B starts "select ..." but does not get the previously inserted
row. The timespan between commit and select is very short.
(NOTE: two different sessions are used)
Questions:
1.) Does commit when returning from call...
|
by: Profetas |
last post by:
I have the following code that detects a
<c>
and
</c>
#include <stdio.h>
main(int argc, char *argv)
{
FILE* fp;
char data;
|
by: Rolf Schroedter |
last post by:
(Sorry for cross-posting).
I need to access large files > 2GByte (Linux, WinXP/NTFS)
using the standard C-library calls.
Till today I thought I know how to do it, namely for
Win32:
Use open(), read(), _itelli64(), _lseeki64() with type __int64
Linux/Cygwin:
#define _FILE_OFFSET_BITS 64
Use open(), read(), lseek() with type off_t
|
by: Naren |
last post by:
Hello All,
Can any one help me in this file read problem.
#include <stdio.h>
int main()
{
|
by: ferbar |
last post by:
Hi all,
I'm trying to read from the txt file 'ip.packets.2.txt' using the read
function. It seems everything ok, but I get a -1 when executing
>>bytesr = read(fdo1, bufread, 2);
The 'open' function returns the file dsc 3. So this seems ok..
Any idea what might be wrong?
| |
by: james |
last post by:
I have a FileStream retrieved from FileOpenDialog. I have a Byte which I
intend to store for later use. What is the most efficient way of getting
the File into my Byte and tehn back out to a new FileStream ? I have been
looking at all the Readers and Writers and due to information overload
cannot decide on the proper method.
Thanks,
JIM
|
by: a |
last post by:
I have a struct to write to a file
struct _structA{
long x;
int y;
float z;
}
struct _structA A;
//file open
write(fd,A,sizeof(_structA));
//file close
|
by: Danil Dotsenko |
last post by:
Wrote a little "user-friedly" wrapper for ConfigParser for a KDE's
SuperKaramba widget.
(http://www.kde-look.org/content/show.php?content=32185)
I was using 2.4.x python docs as reference and
ConfigParser.read('non-existent-filename') returns in 2.4.x
One user with 2.3.x reported an error stemming from my use of
len(cfgObject.read('potentially-non-existent-filename'))
|
by: asit dhal |
last post by:
hello friends,
can anyone explain me how to use read() write() function in C.
and also how to read a file from disk and show it on the monitor using
onlu read(), write() function ??????
|
by: Cintury |
last post by:
Hi all,
I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql server mobile ce database. Until recently I was synching everything thru a com port serial cable. The devices would connect to the computer thru activesync and are able to acquire an internet connection. The sync for the program occurs thru a website...
|
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...
| |
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,...
|
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |