import gevent


def foo():

    print("Running in foo")
    gevent.sleep(2)
    print("Explicit context switch to foo again")


def bar():
    print("Explicit context to bar")
    gevent.sleep(1 )
    print("Implicit context switch back to bar")

def func3():
    print("running func3")
    gevent.sleep(0)#遇到io就切换,sleep最长2秒,所以整个程序花费两秒,如果是串行需要花费3秒
    print("running func3 again")

gevent.joinall([

    gevent.spawn(foo),#启动一个协程
    gevent.spawn(bar),
    gevent.spawn(func3)
]

)

 

相关文章:

  • 2021-08-28
  • 2021-08-29
  • 2021-09-01
  • 2021-10-06
  • 2021-08-06
  • 2022-12-23
  • 2021-06-24
  • 2022-12-23
猜你喜欢
  • 2021-08-09
  • 2022-12-23
  • 2021-08-27
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案