473,385 Members | 1,593 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,385 software developers and data experts.

how to run an arbitrary function with timeout?


i'm building a test suite on top of unittest, and some
of the tests involve things that might hang, like trying
to connect to a wedged server. so i'd like a simple function
that i can call that will run a given (func,args) pair and
return either the value or raise an exception if it times
out. this seems like it should be straightforward, but i've
not had much luck getting it to work.

my latest attempt, below, raises the exception ok,
but still doesn't return until snooze() completes:

--> xx
going to sleep
Traceback (most recent call last):
File "./xx", line 26, in ?
print RunWithTimeout( snooze, (10,), 2 )
File "./xx", line 16, in RunWithTimeout
raise TookTooLong, 'fsdfsdf'
__main__.TookTooLong: fsdfsdf

....8 second delay here...

waking up
can someone tell me what i'm doing wrong?

thanks

------------------------
#!/usr/bin/env python2.3

from threading import *
from time import sleep

class TookTooLong( Exception ):
pass

def RunWithTimeout( func, args, timeout ):
t = Thread( target=func, args=args )
t.start()
t.join( timeout )

if t.isAlive():
del t
raise TookTooLong, 'fsdfsdf'
return 'ok'
def snooze( duration ):
print 'going to sleep'
sleep( duration )
print 'waking up'

if __name__ == '__main__':
print RunWithTimeout( snooze, (10,), 2 )

----
Garry Hodgson, Technology Consultant, AT&T Labs

Be happy for this moment.
This moment is your life.

Jul 18 '05 #1
0 1458

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
1
by: deko | last post by:
I have 3 files I need to write to - and need to make sure I have a lock on each, and take action if I can't get a lock. The code below works, but I know it could be more efficient if I used a...
4
by: Radioactive Man | last post by:
anyone know of a function like "raw_input", which collects a string from the user entry, but one where I can set a time limit, as follows: time_limit = 10 # seconds user_answer =...
1
by: Mike | last post by:
I want to pass the address of a function to a timeout type class so that it can be used as a callback function. The function I want to use as a callback is defined like... void...
38
by: Lasse Vågsæther Karlsen | last post by:
After working through a fair number of the challenges at www.mathschallenge.net, I noticed that some long-running functions can be helped *a lot* by caching their function results and retrieving...
1
by: Samuel Stanojevic | last post by:
- I'm using .NET Framework 1.1. I just noticed that whenever I change the value of Session.Timeout (no matter which aspx page this happens on), at the end of the request, the 'Session_End'...
12
by: Chadwick Boggs | last post by:
I need to perform modulo operations on extremely large numbers. The % operator is giving me number out of range errors and the mod(x, y) function simply seems to return the wrong results. Also,...
5
by: Steven D'Aprano | last post by:
I have a problem and I don't know where to start looking for a solution. I have a class that needs to call an arbitrary function and wait for a result. The function, being completely arbitrary...
2
by: darknails | last post by:
Hi, I would like to know how to define a arbitrary mathematical function with one variable and a list of paramter, like, f(x). For example, in GSL library this is done as /* Definition of an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.