[英]Stop a blocking operation after 10 seconds?
我目前正在編寫一個 Python 2.7 程序,該程序使用 Mechanize 查詢網站的某些 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() 操作在 10 秒后沒有返回,則停止等待 read() 並跳轉到查詢下一個 URL。 如何計時 read() 操作並查看 10 秒后content
是否為空? 我正在考慮使用線程,但不確定如何進行。 任何人都可以幫忙嗎? 提前致謝!
嘗試使用這里描述的超時裝飾器 - https://pypi.python.org/pypi/timeout-decorator
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.