import greenlet # 绿色线程 (实现协程的一种方式) import time def task1(): print("task1 run") g2.switch() print("task1 over") g2.switch() def task2(): print("task2 run") g1.switch() time.sleep(1) print("task2 over") g1 = greenlet.greenlet(task1) g2 = greenlet.greenlet(task2) g1.switch() print("主over") # task1 run # task2 run # task1 over # task2 over # 主over