(24)协程---joinall和value

# spawn(函数,参数...) 启动一个协成

# join()  阻塞,直到某个协程执行完毕

# joinall 类似于join 只不过

g1.join()

g2.join()

gevent.joinall([g1,g2]) 一次性把所有需要阻塞的协程对象写到一起

# value 获取协成的返回值

import time, gevent
def eat():
    print("eating 111")
    time.sleep(1)
    print("eating 222")
    return "吃完了"

def play():
    print("play 111")
    time.sleep(1)
    print("play 222")
    return "play done"

g1 = gevent.spawn(eat)
g2 = gevent.spawn(play)
gevent.joinall([g1, g2])
print(g1.value)
print(g2.value)
View Code

执行结果:

eating 222
play 111
play 222
吃完了
play done
View Code

 

转载于:https://www.cnblogs.com/lyj910313/p/10793249.html

你可能感兴趣的:((24)协程---joinall和value)