我有两个函数从两个不同的连接接收数据,我应该在从其中一个连接获得结果后关闭这两个连接。
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
我该怎么做?