web hosting and development forums
Web Design and Development Forums Archive


Index - Python programming forum

how to build a timeout in a function?


Post reply

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?
Depends on what else the function does.
Klimapol or what context your using the function in??
Klimapol, it fetches a tarball from the web using httplib
Lusis Backwood JR, And are you using any other framework or GUI or similar?
Klimapol, nope I do not
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?
Klimapol, i'd like to abort it when it's exceeding X seconds of time
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




Dubai Forum | Paris Forum | Webmaster Forum | Vegan Forum | Brisbane Forum | 3D Forum | Jobs in Dubai | Jobs in London | London UK Classifieds
Archive script by RedHo.com