pip install greenlet
# -*- coding: utf-8 -*-
from greenlet import greenlet
def func1():
print("func1 first")
gr2.switch()
print("func2 second")
gr2.switch()
def func2():
print("func2 first")
gr1.switch()
print("func2 second")
gr1 = greenlet(func1)
gr2 = greenlet(func2)
gr1.switch()
执行结果:
func1 first
func2 first
func2 second
func2 second