开发者

What is the Python equivalent of PHP's set_time_limit()?

开发者 https://www.devze.com 2022-12-15 15:45 出处:网络
I have a python script which is freezing (I think it stalls waiting for socket data somewhere), but I am having trouble getting a backtrace because the only way to stop 开发者_Go百科it is to kill the

I have a python script which is freezing (I think it stalls waiting for socket data somewhere), but I am having trouble getting a backtrace because the only way to stop 开发者_Go百科it is to kill the process in. There is a timeout on the socket also, but it doesn't seem to work.

I am hoping that Python has a feature like PHP's set_time_limit() function which can stop the script and give me a useful backtrace, perhaps showing a sock.recv() call which is frozen, or an endless loop somewhere.


signal.alarm can help (on Unix platforms), but (depending on the platform) there may be uninterruptable system calls (and if I get the docs right, on Unix, PHP's set_time_limit does not count time spent in system calls, so a hanging system call would be a problem there too).


You could set a timeout on your sockets instead.

import socket
socket.setdefaulttimeout(10) #10 seconds

Do that at the start of your program and everything making socket connections should respect it.

0

精彩评论

暂无评论...
验证码 换一张
取 消