• 协程


     1 def f():
     2     print('ok')
     3     a = yield 666
     4     print(a)
     5     print('ok2')
     6     yield
     7 
     8 
     9 gen = f()
    10 a = gen.__next__()
    11 print(a)
    12 gen.send(555)
    13 输出:
    14 ok
    15 666
    16 555
    17 ok2

    通过使用yield变为一个迭代器,yield以上为一个元素。   send()也相当于调用next方法,不过也可以向yield传递值。

     1 from greenlet import greenlet
     2 
     3 def task1():
     4     print(1)
     5     gr2.switch()
     6     print(2)
     7     gr2.switch()
     8 
     9 
    10 def task2():
    11     print(3)
    12     gr1.switch()
    13     print(4)
    14     gr1.switch()
    15 
    16 
    17 
    18 if __name__ == '__main__':
    19     gr1 = greenlet(task1)
    20     gr2 = greenlet(task2)
    21 
    22     gr1.switch()
    23 输出:
    24 1
    25 3
    26 2
    27 4

    协程面向的主要是IO操作

  • 相关阅读:
    Splash wait() 方法
    Splash go() 方法
    Splash 对象方法
    短信接口文档
    WMS开发环境
    Eureka
    pom.xml settings.xml
    Druid
    EAI并发
    重启WMS服务
  • 原文地址:https://www.cnblogs.com/ch2020/p/12763332.html
Copyright © 2020-2023  润新知