我目前正在编写一个Python 2.7程序,它使用Mechanize来查询某个网站的一些URL信息。伪码是这样的:10秒后停止阻止操作?
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()并跳转到查询下一个URL,如果当前的read()操作在10秒后没有返回。如何计时read()操作并查看content
在10秒后是否为空?我正在考虑使用线程,但不知道如何继续。谁能帮忙?提前致谢!
使用'select' .... –
我猜你正在使用的urllib2?看到这个答案:http://stackoverflow.com/questions/16646322/setting-the-timeout-on-a-urllib2-request-call –
@MikePlacentra对不起,我刚刚更新我的帖子,我使用机械化,而不是urllib2。谢谢 – fittaoee