There is a socket related function call in my code, that function is from another module thus out of my control, the problem is that it blocks for hours occasionally, which is totally unacceptable, How can I limit the function execution time from my code? I guess the solution must utilize another thread.
解决方案
I'm not sure how cross-platform this might be, but using signals and alarm might be a good way of looking at this. With a little work you could make this completely generic as well and usable in any situation.
So your code is going to look something like this.
importsignaldefsignal_handler(signum,frame):raiseException("Timed out!")signal.signal(signal.SIGALRM,signal_handler)signal.alarm(10)# Ten secondstry:long_function_call()exceptException,msg:print"Timed out!"