473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Fork+Timeout

Hello,

do someone new how I can do the following?

I call a method which reads from a socket. So ... this method could hang
forever and so my call hangs too ...

So ... I can not change the behavior of this Method, so I have to change my
call.

I though by forking the call to this function and set a timeout to this
call, so my program won't hang forever.

But I don't know how I should do this and if this is the correct/best way to
do this.

Best regards
Oliver
Jul 18 '05 #1
2 1675
> do someone new how I can do the following?

I call a method which reads from a socket. So ... this method could hang
forever and so my call hangs too ...

So ... I can not change the behavior of this Method, so I have to change my
call.

I though by forking the call to this function and set a timeout to this
call, so my program won't hang forever.

But I don't know how I should do this and if this is the correct/best way to
do this.


Use non-blocking sockets. Check out the asyncore module, or if you are
feeling ambitious, check out Twisted.

- Josiah
Jul 18 '05 #2
In article <c4**********@news.web.de>, "Oliver Kurz" <ol**@web.de>
wrote:
do someone new how I can do the following?

I call a method which reads from a socket. So ... this method could hang
forever and so my call hangs too ...

So ... I can not change the behavior of this Method, so I have to change my
call.

I though by forking the call to this function and set a timeout to this
call, so my program won't hang forever.

But I don't know how I should do this and if this is the correct/best way to
do this.


You might be able to get something like that to work. If you
want to give it a try, I would start by learning to use
signal.alarm(), and then see if its SIGALRM signal will
interrupts a socket recv() on your platform. You won't need
fork, I don't think.

You can probably do better, though, if you can get at the
socket. My favorite way to approach this kind of situation
is to wait for I/O on all relevant devices with select.select().
When select says your method's socket is readable, you can
call the method, and it will return right away.

You can probably do better, though. I like select.select(),
which waits for I/O on any of a collection of devices. That
will tell you when it's safe to call your method.

It will work only if 1) you have access to the socket object,
2) the method doesn't use fileobject I/O on the socket (because
its buffering confuses the issue), and 3) the other inputs to
your program work with select. (Oh, and if you're on UNIX.)
If it's an X11 graphical application, you'll want to use the
external I/O dispatching mechanism provided by your X toolkit
instead.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #3

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

Similar topics

2
by: Markus Franz | last post by:
Hi. My little Python script creates some child-processes depending on how much command line options were given: for myvalue in sys.argv:
5
by: Jim | last post by:
Hi, I am trying to figure out a way to implement a timeout along with a read() call on an open file. It only has to work on linux, for now I am trying: ret = select.select( ,,, timeout ) if...
5
by: Natan | last post by:
Hi. I have a python script under linux where I poll many hundreds of interfaces with mrtg every 5 minutes. Today I create some threads and use os.system(command) to run the process, but some of...
1
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;
1
by: Alexander N. Spitzer | last post by:
I am trying to write a program that will fork a process, and execute the given task... the catch is that if it runs too long, I want to clean it up. this seemed pretty straight forward with a...
4
by: rh0dium | last post by:
Hi all, I have a problem with putting a job in the background. Here is my (ugly) script which I am having problems getting to background. There are threads about doing python script.py & ...
12
by: Sophie000 | last post by:
I am implementing a peer-to-peer program: When a computer A receives “get file XXX” from stdio, it broadcasts (floods) a request packet to ask others. Both Computer B and C have the file so they...
1
by: webotronics | last post by:
Hi, I need to add a timeout for external programs, as the external program sometimes never dies (it's a ClearQuest multisite call to the shipping server, that sometimes never ends, but simply...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...
0
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
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
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,...

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.