redho home | products | services

Programming Forums


Community for Java, PHP, Perl C, ASP and Python programmers
Monday 06 September 2010 15:04

Ask your IT question here

how to build a timeout in a function?



 
Post new topic   Reply to topic    Programming Forums -> Python programming forum
View previous topic :: View next topic  
Author Message
Lusis Backwood JR
Enthusiastic Coder


Joined: 26 May 2006
Posts: 87
Location: United States
Hello.
I'd like to build a timeout in a function. I tried this by doing a while loop and check the time for X difference. Of course, this took 100% of the processor. Are there better ways to create such feature?

Reply with quote
 
Klimapol
Enthusiastic Coder


Joined: 27 May 2006
Posts: 134
Depends on what else the function does.
Reply with quote
 
amoralis
New Programmer


Joined: 22 Jun 2006
Posts: 21
Klimapol or what context your using the function in??
Reply with quote
 
Lusis Backwood JR
Enthusiastic Coder


Joined: 26 May 2006
Posts: 87
Location: United States
Klimapol, it fetches a tarball from the web
using httplib

Reply with quote
 
Klimapol
Enthusiastic Coder


Joined: 27 May 2006
Posts: 134
Lusis Backwood JR, And are you using any other framework or GUI or similar?
Reply with quote
 
Lusis Backwood JR
Enthusiastic Coder


Joined: 26 May 2006
Posts: 87
Location: United States
Klimapol, nope I do not
Reply with quote
 
Klimapol
Enthusiastic Coder


Joined: 27 May 2006
Posts: 134
And do you want to just abort if more than X seconds are used, or do you want to be called back every so often when data is fetched?
Reply with quote
 
Lusis Backwood JR
Enthusiastic Coder


Joined: 26 May 2006
Posts: 87
Location: United States
Klimapol, i'd like to abort it when it's exceeding X seconds of time
Reply with quote
 
Klimapol
Enthusiastic Coder


Joined: 27 May 2006
Posts: 134
If you just want to avoid stalling, the ideal way is to use e.g. twisted which lets you send off a request while doing other thing at the same time, i.e. you schedule the page to be fetched and schedule a timeout, and run the main loop
A low-tech solution is to use socket.setdefaulttimeout which lets you set a default timeout on socket operations
You can set the timeout per-socket too, but urllib doesn't expose the socket it uses AFAIK
Unfortunately that solution won't let you timeout on nameserver lookups.
An alternative is to run the fetching in a separate thread but then you can't really abort it if it takes more than the wanted amount of time

Reply with quote
 
Page 1 of 1
Post new topic   Reply to topic    Programming Forums -> Python programming forum


Dubai Forums - Expat Help | Vegan Forum | Java Programming | 3d Design Resources | 3d Forum | 3D Jobs | 3D Textures | Paris Forum | Europe Forum
Dubai Classifieds | Dubai Property | Jobs in Dubai | Free London Classifieds | Jobs in London UK

High Quality, Custom 3d animation and Web Design solutions Royal Quality Web Hosting Services Vegetarian and Animal Rights news

Based on phpBB © 2001-2010 RedHo