zoukankan      html  css  js  c++  java
  • python 协程

    '''协成、对于非抢占式的调度程序 
    1.与生成器 yleid()、保存状态
    2.用户态的切换、切换由用户决定
    3.协程本身是一种微线程
    4.协成也是面向IO操作
    5.没有切换的消耗
    6.没有锁的概念
    '''

    (1)生成器函数

    def f():
        print("ok")
        s= yield 5
        print(s)
        print("ok2")
        yield
    
    gen=f()
    x=gen.__next__()
    print(x)
    gen.send(0)

    (2)生产者消费者模型用yeild实现

    import queue
    import time
    q=queue.Queue()
    
    def Cousumer(name):
        print("准备开始吃包子>>>>>>>>")
        while True:
            new_baozi = yield
            print("cousumer %s has eat %s baozi" %(name,new_baozi))
    
    def Producer(name):
        r = con1.__next__()
        r = con2.__next__()
        n = 0
        while 1:
            time.sleep(1)
            print("Producer %s  is making baozi %s and %s" %(name,n,n+1))
            con1.send(n)
            con2.send(n+1)
            n +=2
    if __name__ == '__main__':
       con1 =Cousumer("c1")
       con2 = Cousumer("c2")
       p = Producer("1号厨师")

    (3)-------------greenlet 函数间的切换更加方便--------------------------

    #通过 switch() 随时切换
    from greenlet import greenlet
    def too2():
        print(898)
        gr1.switch()
        print(34)
    def too1():
        print(89)
        gr2.switch()
        print("7897978")
        gr2.switch()
    
    
    gr1=greenlet(too1)
    gr2=greenlet(too2)
    
    # gr1.switch() # 动作的发起端
    too1()

    (4)-----------------gevent接口遇到IO等待操作实现自动切换---------------------

    import requests,time,gevent
    t1=time.time()
    def f(url):
        print("GET: %s" % url)
        resp =requests.get(url)
        data=resp.text
        f= open("new.html","w",encoding="utf8")
        f.write(data)
        print("%d bytes recevied from %s" %(len(data),url))
    
    gevent.joinall([
                   gevent.spawn(f,"https://www.bilibili.com/video/av21663728/?p=325"),
                   gevent.spawn(f,"http://news.baidu.com/"),
                   gevent.spawn(f,"http://www.okoook.com/"),
                   gevent.spawn(f,"https://www.imooc.com/"),
                   gevent.spawn(f,"http://www.xiaohuar.com/"),
                   gevent.spawn(f,"http://www.xiaohuar.com/hua/"),
                  ])
    # print(time.time()-t1)
    
    #比较串行
    # f("https://www.bilibili.com/video/av21663728/?p=325")
    # f("http://news.baidu.com/")
    #f("http://www.okoook.com/")
    # f("https://www.imooc.com/")
    # f("http://www.xiaohuar.com/")
    # f("http://www.xiaohuar.com/hua/")
    
    print(time.time()-t1)
  • 相关阅读:
    3.Appium运行时出现:Original error: Android devices must be of API level 17 or higher. Please change your device to Selendroid or upgrade Android on your device
    3.Python连接数据库PyMySQL
    2.Python输入pip命令出现Unknown or unsupported command 'install'问题解决
    2.Linux下安装Jenkins
    5.JMeter测试mysql数据库
    Android 4学习(7):用户界面
    Android 4学习(6):概述
    Android 4学习(5):概述
    Android 4学习(4):概述
    Android 4学习(3):概述
  • 原文地址:https://www.cnblogs.com/tsgxj/p/9256052.html
Copyright © 2011-2022 走看看