import requests
import eventlet
eventlet.monkey_patch()
with eventlet.Timeout(2,False):
r=requests.get("http://blog.youkuaiyun.com/happyanger6/article/details/54766559", verify=False)
print('r')
print('aa')
Ref:
https://stackoverflow.com/questions/21965484/timeout-for-python-requests-get-entire-response
https://www.cnblogs.com/Security-Darren/p/4170031.html
eventlet针对子进程无法跳出
更新其他方式:
方法一:from threading import Timer,但是由于timer是另开的一个线程,所以这个异常别人获取不到,只有在本线程里处理才有用,而且卡死的函数也不会停下来。
方法二:通过func_timeout模块的@func_set_timeout()来实现
方法三:使用signal设置装饰器