我目前正在编写一个Python2.7程序,它使用机器查询网站的一些URL信息。伪代码如下所示:
for URL in urls:
# first submit a form to query the URL...
# then wait and parse the results:
while 1:
content = response.read(1024)
# followed by some other simple parsing operations ...while 1循环中的read()操作是阻塞的,由于网络和其他条件,有时它会永远运行。我试图实现的是停止等待read(),如果当前read()操作10秒后未返回,则跳到查询下一个URL。如何计时read()操作,并在10秒后查看content是否为空?我正在考虑使用线程,但不知道如何继续。有人能帮忙吗?提前感谢!
发布于 2017-01-20 20:50:37
尝试使用这里描述的超时装饰器- https://pypi.python.org/pypi/timeout-decorator
https://stackoverflow.com/questions/41772072
复制相似问题