| View previous topic :: View next topic |
| Author |
Message |
Lusis Backwood JR
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? |
| |
|
|
|
|
Klimapol
Joined: 27 May 2006 Posts: 134
|
| Depends on what else the function does. |
| |
|
|
amoralis
Joined: 22 Jun 2006 Posts: 21
|
| Klimapol or what context your using the function in?? |
| |
|
|
Lusis Backwood JR
Joined: 26 May 2006 Posts: 87 Location: United States
|
Klimapol, it fetches a tarball from the web
using httplib |
| |
|
|
Klimapol
Joined: 27 May 2006 Posts: 134
|
| Lusis Backwood JR, And are you using any other framework or GUI or similar? |
| |
|
|
Lusis Backwood JR
Joined: 26 May 2006 Posts: 87 Location: United States
|
| Klimapol, nope I do not |
| |
|
|
Klimapol
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? |
| |
|
|
Lusis Backwood JR
Joined: 26 May 2006 Posts: 87 Location: United States
|
| Klimapol, i'd like to abort it when it's exceeding X seconds of time |
| |
|
|
Klimapol
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 |
| |
|
|
|