簡體   English   中英

使用python時gevent中發生了線程異常

[英]thread exception occurred in gevent when using python

當我運行這個python代碼,但發生了一些線程問題。 我搜索互聯網,但我找不到答案。

import urllib2
from time import time
import gevent
import numpy as np

tmp = np.loadtxt('data.txt', dtype=str)
visit_list=tmp[:20]

result_list =[]

def visitUrl(u):
  print 'start %s' %u
  begin = time()
  data = urllib2.urlopen(u).read()
  print len(data)
  print 'end-------- %s' %u
  end = time()
  print ('%s ::begin=%s ::end= %s::end-begin= %s' %(u, begin,end, end-begin))

for i in range(3):
    reqs = []
    begin = time()
    for u in visit_list:
        start = time()
        reqs.append(gevent.spawn(visitUrl, u))
        stop = time()
        print ('%s $$$ %s' %(u, stop-start))
    #reqs = [gevent.spawn(visitUrl, u) for u in visit_list]
    gevent.joinall(reqs)
    end = time()

    print 'ciclie %s=%s' %(i, end - begin)
    result_list.append(end-begin)

問題:

Exception KeyError: KeyError(4475468392,) in <module 'threading' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.pyc'> ignored

根據這個線程,你的主循環在所有線程完成其工作之前結束。

只需在結束時調用gevent.shutdown()或在調用joinall增加超時:

gevent.joinall(reqs, timeout=30)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM