1

我有两个函数从两个不同的连接接收数据,我应该在从其中一个连接获得结果后关闭这两个连接。

def first():
    gevent.sleep(randint(1, 100))  # i don't know how much time it will work
    return 'foo'

def second():
    gevent.sleep(randint(1, 100))  # i don't know how much time it will work
    return 'bar'

然后我生成每个函数:

lst = [gevent.spawn(first), gevent.spawn(second)]

gevent.joinall阻塞当前的greenlet,直到两个greenletlst都准备好。

gevent.joinall(lst)  # wait much time
print lst[0].get(block=False)   # -> 'foo'
print lst[1].get(block=False)   # -> 'bar'

我想等到第一个或第二个greenlet准备好:

i_want_such_function(lst)  # returns after few seconds
print lst[0].get(block=False)  # -> 'foo' because this greenlet is ready
print lst[1].get(block=False)  # -> raised Timeout because this greenlet is not ready

我该怎么做?

4

2 回答 2

4

您可以使用 gevent.event.Event(或 AsyncResult)和 Greenlet 的 link() 方法,如下所示:

...
ready = gevent.event.Event()
ready.clear()

def callback():
    ready.set()

lst = [gevent.spawn(first), gevent.spawn(second)]
for g in lst:
    g.link(callback)

ready.wait()
...
于 2012-11-28T09:40:37.650 回答
0

回调接收 gevent 子进程,您可以从中获取例如返回值

...

cars = []

def _callback(job):
    cars.append(job.value)

for car in xml_all_cars:
    print "creating jobs"
    g_parse = Greenlet.spawn(myMainFunction)
    g_parse.start()
    g_parse.link(_callback)
    jobs.append(g_parse)

print "starting all jobs"
gevent.joinall(jobs)
print "jobs done"

return cars

汽车将具有 myMainFunction 返回的所有值的列表(对于每辆车)

于 2015-05-11T14:42:11.430 回答