[Tutor] breaking out of a function that takes too long
Kent Johnson
kent37 at tds.net
Tue Sep 15 23:44:43 CEST 2009
On Tue, Sep 15, 2009 at 5:02 PM, Serdar Tumgoren <zstumgoren at gmail.com> wrote:
> Hey everyone,
> Is there a way to break out of a function if it exceeds a certain time
> limit for execution?
In general this is tricky. It usually involves setting up another
thread to run or monitor the function, then somehow terminating the
thread if it runs too long.
In your case, it sounds like a better solution is to set a socket
timeout. At the beginning of your program - before attempting any
access to the website - put
import socket
socket.setdefaulttimeout(<timeout in seconds>)
This will cause the web site fetch to fail by raising socket.timeout
if the fetch blocks for longer than the timeout seconds.
Kent
More information about the Tutor
mailing list