1 from greenlet import greenlet
 2 
 3 def test1():
 4     print(12)
 5     g2.switch()#切换到协程g2执行,保存执行状态
 6     print(23)
 7     g2.switch()#切换到协程g2执行,保存执行状态
 8     print(34)
 9 def test2():
10     print(22)
11     g1.switch()#切换到协程g1执行,保存执行状态
12     print(33)
13     g1.switch()#切换到协程g1执行,并保存执行状态
14     print(44)
15 
16 
17 
18 
19 g1 = greenlet(test1)#启动协程g1
20 g2 = greenlet(test2)#启动协程g2
21 g1.switch()
总结:个人认为协程就是使用的生成器状态,或者说类似于生成器状态,而执行就是使用的.__next__命令内部实现。并实现状态保存。因为是在同一进程里,所以不需要加锁。

 

 

 

 

相关文章:

  • 2021-08-06
  • 2018-06-13
  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
  • 2021-09-01
  • 2022-03-05
  • 2021-10-30
猜你喜欢
  • 2021-08-04
  • 2021-08-29
  • 2021-08-09
  • 2022-12-23
  • 2019-10-23
  • 2022-03-05
相关资源
相似解决方案