多请求pycurl永远运行(无限循环)
我想使用 Pycurl 执行多请求。代码是: m.add_handle(句柄) requests.append((handle, response))
# Perform multi-request.
SELECT_TIMEOUT = 1.0
num_handles = len(requests)
while num_handles:
ret = m.select(SELECT_TIMEOUT)
if ret == -1: continue
while 1:
ret, num_handles = m.perform()
print "In while loop of multicurl"
if ret != pycurl.E_CALL_MULTI_PERFORM: break
事实是,这个循环需要永远运行。它没有终止。 谁能告诉我它的作用以及可能出现的问题是什么?
I want to perform Multi-request using Pycurl. Code is:
m.add_handle(handle)
requests.append((handle, response))
# Perform multi-request.
SELECT_TIMEOUT = 1.0
num_handles = len(requests)
while num_handles:
ret = m.select(SELECT_TIMEOUT)
if ret == -1: continue
while 1:
ret, num_handles = m.perform()
print "In while loop of multicurl"
if ret != pycurl.E_CALL_MULTI_PERFORM: break
Thing is, this loop takes forever to run. Its not terminating.
Can any one tell me, what it does and what are the possible problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你看过 PyCurl 官方代码吗?下面的代码实现了多种功能,我尝试执行它,并且能够在 300 秒内并行抓取大约 10,000 个 URL。我想这正是您想要实现的目标?如果我错了,请纠正我。
Did you go through PyCurl official codes? The following code implement multi stuff and I tried executing it and I was able to crawl around 10,000 urls in 300 secs in parallel. I think this is exactly what you want to achieve? Correct me, if am wrong.
我认为这是因为你只跳出了第一个 while 循环,
所以如果你使用“break”会发生什么,你将跳出当前的 while 循环(当你使用break时,你处于第二个 while 循环中。)
该程序的下一步将接收此处写为“**”的行,因为这是它跳回的最后一行。
(到 while num_handles 中的第一行)
然后再往下 3 行,它会遇到 'while 1:' 等等......这就是你得到 inf 循环的方式。
所以解决这个问题的方法是:
所以这里发生的事情是,一旦它脱离了嵌套的 while 循环,它也会自动脱离第一个循环。
(并且由于
while
和之前使用的continue
,它永远不会到达该行I think it is because you only break out of the first while loop
so what happens if you use 'break', you will break out of the current while loop (you are in the second whileloop when you use break.)
next step for the program would to take in the line written '**' here, since it's the last line it jumps back.
(to the first line in the while num_handles)
and then 3 lines further it runs into 'while 1:' and soforth.. and that's how you get the inf loop.
so to fix this would be:
so what happens here, is as soon as it break's out of the nested while loop, it will automatically break out of the first loop too.
(and it would never reach the line otherwise because of the
while
, and thecontinue
used before