我希望下面的代码使用 10 个并发线程并在池释放时完成其余的。相反,额外的调用会导致线程崩溃,就好像即使 urllib 请求没有成功,被调用的函数也试图继续。但为什么?我认为在游泳池为绿灯打开之前不会调用整个函数。或者即使该函数被提前调用,是什么阻止了它完成?
FWIW:workrow 函数(未显示)获取列表(CSV 的一行),在 Web 上发出 API 请求,解析 JSON,然后在另一个 CSV 中写入一行。
import gevent
from gevent import socket
from gevent import monkey, pool
# patches stdlib (including socket and ssl modules) to cooperate with other greenlets
monkey.patch_all()
p = pool.Pool(10)
with open(inputfile, 'rb') as csvfile:
entreader = unicodecsv.reader(csvfile, delimiter=',', quotechar='"')
head=[entreader.next() for x in xrange(20)]
for row in head:
p.spawn(workrow, row)
p.join()
错误:
Traceback (most recent call last):
File "/Users/laszlosandor/anaconda/lib/python2.7/site-packages/gevent/greenlet.py", line 327, in run
result = self._run(*self.args, **self.kwargs)
File "/Users/laszlosandor/Downloads/GoogleGeocoding/google_geocoding_LS.py", line 37, in workrow
result = data_json["results"][0]
IndexError: list index out of range
<Greenlet at 0x101245c30: workrow([u'p_366937', u'/entity/p/366937.xml', u'H\xe9derv)> failed with IndexError