• Python黑魔法 --- 异步IO( asyncio) 协程


    网络模型有很多中,为了实现高并发也有很多方案,多线程,多进程。无论多线程和多进程,IO的调度更多取决于系统,而协程的方式,调度来自用户,用户可以在函数中yield一个状态。使用协程可以实现高效的并发任务。Python的在3.4中引入了协程的概念,可是这个还是以生成器对象为基础,3.5则确定了协程的语法。下面将简单介绍asyncio的使用。实现协程的不仅仅是asyncio,tornado和gevent都实现了类似的功能。

    • event_loop 事件循环:程序开启一个无限的循环,程序员会把一些函数注册到事件循环上。当满足事件发生的时候,调用相应的协程函数。
    • coroutine 协程:协程对象,指一个使用async关键字定义的函数,它的调用不会立即执行函数,而是会返回一个协程对象。协程对象需要注册到事件循环,由事件循环调用。
    • task 任务:一个协程对象就是一个原生可以挂起的函数,任务则是对协程进一步封装,其中包含任务的各种状态。
    • future: 代表将来执行或没有执行的任务的结果。它和task上没有本质的区别
    • async/await 关键字:python3.5 用于定义协程的关键字,async定义一个协程,await用于挂起阻塞的异步调用接口。

    上述的概念单独拎出来都不好懂,比较他们之间是相互联系,一起工作。下面看例子,再回溯上述概念,更利于理解。

    定义一个协程

    定义一个协程很简单,使用async关键字,就像定义普通函数一样:

    通过async关键字定义一个协程(coroutine),协程也是一种对象。协程不能直接运行,需要把协程加入到事件循环(loop),由后者在适当的时候调用协程。asyncio.get_event_loop方法可以创建一个事件循环,然后使用run_until_complete将协程注册到事件循环,并启动事件循环。因为本例只有一个协程,于是可以看见如下输出:

    1 Waiting:  2
    2 TIME:  0.0004658699035644531

    创建一个task

    协程对象不能直接运行,在注册事件循环的时候,其实是run_until_complete方法将协程包装成为了一个任务(task)对象。所谓task对象是Future类的子类。保存了协程运行后的状态,用于未来获取协程的结果。

     1 import asyncio
     2 import time
     3  
     4 now=lambda:time.time()
     5  
     6 async def do_some_work(x):
     7     print('Waiting: ',x)
     8  
     9 start=now()
    10  
    11 coroutine=do_some_work(2)
    12 loop=asyncio.get_event_loop()
    13 # task = asyncio.ensure_future(coroutine)
    14 task=loop.create_task(coroutine)
    15 print(task)
    16 loop.run_until_complete(task)
    17 print(task)
    18 print('TIME: ',now()-start)

    可以看到输出结果为:

    1 <Task pending coro=<do_some_work()running at/Users/ghost/Rsj217/python3.6/async/async-main.py:17>>
    2 Waiting:  2
    3 <Task finished coro=<do_some_work()done,defined at/Users/ghost/Rsj217/python3.6/async/async-main.py:17>result=None>
    4 TIME:  0.0003490447998046875

    创建task后,task在加入事件循环之前是pending状态,因为do_some_work中没有耗时的阻塞操作,task很快就执行完毕了。后面打印的finished状态。

    asyncio.ensure_future(coroutine) 和 loop.create_task(coroutine)都可以创建一个task,run_until_complete的参数是一个futrue对象。当传入一个协程,其内部会自动封装成task,task是Future的子类。isinstance(task, asyncio.Future)将会输出True。

    绑定回调

    绑定回调,在task执行完毕的时候可以获取执行的结果,回调的最后一个参数是future对象,通过该对象可以获取协程返回值。如果回调需要多个参数,可以通过偏函数导入。

     1 import time
     2 import asyncio
     3  
     4 now=lambda:time.time()
     5  
     6 async def do_some_work(x):
     7     print('Waiting: ',x)
     8     return'Done after {}s'.format(x)
     9  
    10 def callback(future):
    11     print('Callback: ',future.result())
    12  
    13 start=now()
    14  
    15 coroutine=do_some_work(2)
    16 loop=asyncio.get_event_loop()
    17 task=asyncio.ensure_future(coroutine)
    18 task.add_done_callback(callback)
    19 loop.run_until_complete(task)
    20  
    21 print('TIME: ',now()-start)
    1 def callback(t,future):
    2     print('Callback:',t,future.result())
    3  
    4 task.add_done_callback(functools.partial(callback,2)

    可以看到,coroutine执行结束时候会调用回调函数。并通过参数future获取协程执行的结果。我们创建的task和回调里的future对象,实际上是同一个对象。

    future 与 result

    回调一直是很多异步编程的恶梦,程序员更喜欢使用同步的编写方式写异步代码,以避免回调的恶梦。回调中我们使用了future对象的result方法。前面不绑定回调的例子中,我们可以看到task有fiinished状态。在那个时候,可以直接读取task的result方法。

     1 async def do_some_work(x):
     2     print('Waiting {}'.format(x))
     3     return'Done after {}s'.format(x)
     4  
     5 start=now()
     6  
     7 coroutine=do_some_work(2)
     8 loop=asyncio.get_event_loop()
     9 task=asyncio.ensure_future(coroutine)
    10 loop.run_until_complete(task)
    11  
    12 print('Task ret: {}'.format(task.result()))
    13 print('TIME: {}'.format(now()-start))

    可以看到输出的结果:

    1 Waiting:  2
    2 Task ret:  Done after2s
    3 TIME:  0.0003650188446044922

    阻塞和await

    使用async可以定义协程对象,使用await可以针对耗时的操作进行挂起,就像生成器里的yield一样,函数让出控制权。协程遇到await,事件循环将会挂起该协程,执行别的协程,直到其他的协程也挂起或者执行完毕,再进行下一个协程的执行。

    耗时的操作一般是一些IO操作,例如网络请求,文件读取等。我们使用asyncio.sleep函数来模拟IO操作。协程的目的也是让这些IO操作异步化。

    import asyncio
    import time
     
    now=lambda:time.time()
     
    async def do_some_work(x):
        print('Waiting: ',x)
        await asyncio.sleep(x)
        return'Done after {}s'.format(x)
     
    start=now()
     
    coroutine=do_some_work(2)
    loop=asyncio.get_event_loop()
    task=asyncio.ensure_future(coroutine)
    loop.run_until_complete(task)
     
    print('Task ret: ',task.result())
    print('TIME: ',now()-start)

    在 sleep的时候,使用await让出控制权。即当遇到阻塞调用的函数的时候,使用await方法将协程的控制权让出,以便loop调用其他的协程。现在我们的例子就用耗时的阻塞操作了。

    并发和并行

    并发和并行一直是容易混淆的概念。并发通常指有多个任务需要同时进行,并行则是同一时刻有多个任务执行。用上课来举例就是,并发情况下是一个老师在同一时间段辅助不同的人功课。并行则是好几个老师分别同时辅助多个学生功课。简而言之就是一个人同时吃三个馒头还是三个人同时分别吃一个的情况,吃一个馒头算一个任务。

    asyncio实现并发,就需要多个协程来完成任务,每当有任务阻塞的时候就await,然后其他协程继续工作。创建多个协程的列表,然后将这些协程注册到事件循环中。

     1 import asyncio
     2  
     3 import time
     4  
     5 now=lambda:time.time()
     6  
     7 async def do_some_work(x):
     8     print('Waiting: ',x)
     9  
    10     await asyncio.sleep(x)
    11     return'Done after {}s'.format(x)
    12  
    13 start=now()
    14  
    15 coroutine1=do_some_work(1)
    16 coroutine2=do_some_work(2)
    17 coroutine3=do_some_work(4)
    18  
    19 tasks=[
    20     asyncio.ensure_future(coroutine1),
    21     asyncio.ensure_future(coroutine2),
    22     asyncio.ensure_future(coroutine3)
    23 ]
    24  
    25 loop=asyncio.get_event_loop()
    26 loop.run_until_complete(asyncio.wait(tasks))
    27  
    28 fortask intasks:
    29     print('Task ret: ',task.result())
    30  
    31 print('TIME: ',now()-start)

    结果如下

    1 Waiting:  1
    2 Waiting:  2
    3 Waiting:  4
    4 Task ret:  Done after1s
    5 Task ret:  Done after2s
    6 Task ret:  Done after4s
    7 TIME:  4.003541946411133

    总时间为4s左右。4s的阻塞时间,足够前面两个协程执行完毕。如果是同步顺序的任务,那么至少需要7s。此时我们使用了aysncio实现了并发。asyncio.wait(tasks) 也可以使用 asyncio.gather(*tasks) ,前者接受一个task列表,后者接收一堆task。

    协程嵌套

    使用async可以定义协程,协程用于耗时的io操作,我们也可以封装更多的io操作过程,这样就实现了嵌套的协程,即一个协程中await了另外一个协程,如此连接起来。

     1 import asyncio
     2  
     3 import time
     4  
     5 now=lambda:time.time()
     6  
     7 async def do_some_work(x):
     8     print('Waiting: ',x)
     9  
    10     await asyncio.sleep(x)
    11     return'Done after {}s'.format(x)
    12  
    13 async def main():
    14     coroutine1=do_some_work(1)
    15     coroutine2=do_some_work(2)
    16     coroutine3=do_some_work(4)
    17  
    18     tasks=[
    19         asyncio.ensure_future(coroutine1),
    20         asyncio.ensure_future(coroutine2),
    21         asyncio.ensure_future(coroutine3)
    22     ]
    23  
    24     dones,pendings=await asyncio.wait(tasks)
    25  
    26     fortask indones:
    27         print('Task ret: ',task.result())
    28  
    29 start=now()
    30  
    31 loop=asyncio.get_event_loop()
    32 loop.run_until_complete(main())
    33  
    34 print('TIME: ',now()-start)

    如果使用的是 asyncio.gather创建协程对象,那么await的返回值就是协程运行的结果。

    1 results=await asyncio.gather(*tasks)
    2  
    3     forresult inresults:
    4         print('Task ret: ',result)

    不在main协程函数里处理结果,直接返回await的内容,那么最外层的run_until_complete将会返回main协程的结果。

     1 async def main():
     2     coroutine1=do_some_work(1)
     3     coroutine2=do_some_work(2)
     4     coroutine3=do_some_work(2)
     5  
     6     tasks=[
     7         asyncio.ensure_future(coroutine1),
     8         asyncio.ensure_future(coroutine2),
     9         asyncio.ensure_future(coroutine3)
    10     ]
    11  
    12     returnawait asyncio.gather(*tasks)
    13  
    14 start=now()
    15  
    16 loop=asyncio.get_event_loop()
    17 results=loop.run_until_complete(main())
    18  
    19 forresult inresults:
    20     print('Task ret: ',result)

    或者返回使用asyncio.wait方式挂起协程。

     1 async def main():
     2     coroutine1=do_some_work(1)
     3     coroutine2=do_some_work(2)
     4     coroutine3=do_some_work(4)
     5  
     6     tasks=[
     7         asyncio.ensure_future(coroutine1),
     8         asyncio.ensure_future(coroutine2),
     9         asyncio.ensure_future(coroutine3)
    10     ]
    11  
    12     returnawait asyncio.wait(tasks)
    13  
    14 start=now()
    15  
    16 loop=asyncio.get_event_loop()
    17 done,pending=loop.run_until_complete(main())
    18  
    19 fortask indone:
    20     print('Task ret: ',task.result())

    也可以使用asyncio的as_completed方法

     1 async def main():
     2     coroutine1=do_some_work(1)
     3     coroutine2=do_some_work(2)
     4     coroutine3=do_some_work(4)
     5  
     6     tasks=[
     7         asyncio.ensure_future(coroutine1),
     8         asyncio.ensure_future(coroutine2),
     9         asyncio.ensure_future(coroutine3)
    10     ]
    11     fortask inasyncio.as_completed(tasks):
    12         result=await task
    13         print('Task ret: {}'.format(result))
    14  
    15 start=now()
    16  
    17 loop=asyncio.get_event_loop()
    18 done=loop.run_until_complete(main())
    19 print('TIME: ',now()-start)

    由此可见,协程的调用和组合十分灵活,尤其是对于结果的处理,如何返回,如何挂起,需要逐渐积累经验和前瞻的设计。

    协程停止

    上面见识了协程的几种常用的用法,都是协程围绕着事件循环进行的操作。future对象有几个状态:

    • Pending
    • Running
    • Done
    • Cancelled

    创建future的时候,task为pending,事件循环调用执行的时候当然就是running,调用完毕自然就是done,如果需要停止事件循环,就需要先把task取消。可以使用asyncio.Task获取事件循环的task

     1 import asyncio
     2  
     3 import time
     4  
     5 now=lambda:time.time()
     6  
     7 async def do_some_work(x):
     8     print('Waiting: ',x)
     9  
    10     await asyncio.sleep(x)
    11     return'Done after {}s'.format(x)
    12  
    13 coroutine1=do_some_work(1)
    14 coroutine2=do_some_work(2)
    15 coroutine3=do_some_work(2)
    16  
    17 tasks=[
    18     asyncio.ensure_future(coroutine1),
    19     asyncio.ensure_future(coroutine2),
    20     asyncio.ensure_future(coroutine3)
    21 ]
    22  
    23 start=now()
    24  
    25 loop=asyncio.get_event_loop()
    26 try:
    27     loop.run_until_complete(asyncio.wait(tasks))
    28 except KeyboardInterrupt ase:
    29     print(asyncio.Task.all_tasks())
    30     fortask inasyncio.Task.all_tasks():
    31         print(task.cancel())
    32     loop.stop()
    33     loop.run_forever()
    34 finally:
    35     loop.close()
    36  
    37 print('TIME: ',now()-start)

    启动事件循环之后,马上ctrl+c,会触发run_until_complete的执行异常 KeyBorardInterrupt。然后通过循环asyncio.Task取消future。可以看到输出如下:

    1 Waiting:  1
    2 Waiting:  2
    3 Waiting:  2
    4 {<Task pending coro=<do_some_work()running at/Users/ghost/Rsj217/python3.6/async/async-main.py:18>wait_for=<Future pending cb=[<TaskWakeupMethWrapper objectat0x101230648>()]>cb=[_wait.<locals>._on_completion()at/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py:374]>,<Task pending coro=<do_some_work()running at/Users/ghost/Rsj217/python3.6/async/async-main.py:18>wait_for=<Future pending cb=[<TaskWakeupMethWrapper objectat0x1032b10a8>()]>cb=[_wait.<locals>._on_completion()at/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py:374]>,<Task pending coro=<wait()running at/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py:307>wait_for=<Future pending cb=[<TaskWakeupMethWrapper objectat0x103317d38>()]>cb=[_run_until_complete_cb()at/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py:176]>,<Task pending coro=<do_some_work()running at/Users/ghost/Rsj217/python3.6/async/async-main.py:18>wait_for=<Future pending cb=[<TaskWakeupMethWrapper objectat0x103317be8>()]>cb=[_wait.<locals>._on_completion()at/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py:374]>}
    5 True
    6 True
    7 True
    8 True
    9 TIME:  0.8858370780944824

    True表示cannel成功,loop stop之后还需要再次开启事件循环,最后在close,不然还会抛出异常:

    1 Task was destroyed but it ispending!
    2 task:<Task pending coro=<do_some_work()done

    循环task,逐个cancel是一种方案,可是正如上面我们把task的列表封装在main函数中,main函数外进行事件循环的调用。这个时候,main相当于最外出的一个task,那么处理包装的main函数即可。

     1 import asyncio
     2  
     3 import time
     4  
     5 now=lambda:time.time()
     6  
     7 async def do_some_work(x):
     8     print('Waiting: ',x)
     9  
    10     await asyncio.sleep(x)
    11     return'Done after {}s'.format(x)
    12  
    13 async def main():
    14     coroutine1=do_some_work(1)
    15     coroutine2=do_some_work(2)
    16     coroutine3=do_some_work(2)
    17  
    18     tasks=[
    19         asyncio.ensure_future(coroutine1),
    20         asyncio.ensure_future(coroutine2),
    21         asyncio.ensure_future(coroutine3)
    22     ]
    23     done,pending=await asyncio.wait(tasks)
    24     fortask indone:
    25         print('Task ret: ',task.result())
    26  
    27 start=now()
    28  
    29 loop=asyncio.get_event_loop()
    30 task=asyncio.ensure_future(main())
    31 try:
    32     loop.run_until_complete(task)
    33 except KeyboardInterrupt ase:
    34     print(asyncio.Task.all_tasks())
    35     print(asyncio.gather(*asyncio.Task.all_tasks()).cancel())
    36     loop.stop()
    37     loop.run_forever()
    38 finally:
    39     loop.close()

    不同线程的事件循环

    很多时候,我们的事件循环用于注册协程,而有的协程需要动态的添加到事件循环中。一个简单的方式就是使用多线程。当前线程创建一个事件循环,然后在新建一个线程,在新线程中启动事件循环。当前线程不会被block。

     1 from threading import Thread
     2  
     3 def start_loop(loop):
     4     asyncio.set_event_loop(loop)
     5     loop.run_forever()
     6  
     7 def more_work(x):
     8     print('More work {}'.format(x))
     9     time.sleep(x)
    10     print('Finished more work {}'.format(x))
    11  
    12 start=now()
    13 new_loop=asyncio.new_event_loop()
    14 t=Thread(target=start_loop,args=(new_loop,))
    15 t.start()
    16 print('TIME: {}'.format(time.time()-start))
    17  
    18 new_loop.call_soon_threadsafe(more_work,6)
    19 new_loop.call_soon_threadsafe(more_work,3)

    启动上述代码之后,当前线程不会被block,新线程中会按照顺序执行call_soon_threadsafe方法注册的more_work方法,后者因为time.sleep操作是同步阻塞的,因此运行完毕more_work需要大致6 + 3

    新线程协程

     1 def start_loop(loop):
     2     asyncio.set_event_loop(loop)
     3     loop.run_forever()
     4  
     5 async def do_some_work(x):
     6     print('Waiting {}'.format(x))
     7     await asyncio.sleep(x)
     8     print('Done after {}s'.format(x))
     9  
    10 def more_work(x):
    11     print('More work {}'.format(x))
    12     time.sleep(x)
    13     print('Finished more work {}'.format(x))
    14  
    15 start=now()
    16 new_loop=asyncio.new_event_loop()
    17 t=Thread(target=start_loop,args=(new_loop,))
    18 t.start()
    19 print('TIME: {}'.format(time.time()-start))
    20  
    21 asyncio.run_coroutine_threadsafe(do_some_work(6),new_loop)
    22 asyncio.run_coroutine_threadsafe(do_some_work(4),new_loop)

    上述的例子,主线程中创建一个new_loop,然后在另外的子线程中开启一个无限事件循环。主线程通过run_coroutine_threadsafe新注册协程对象。这样就能在子线程中进行事件循环的并发操作,同时主线程又不会被block。一共执行的时间大概在6s左右。

    master-worker主从模式

    对于并发任务,通常是用生成消费模型,对队列的处理可以使用类似master-worker的方式,master主要用户获取队列的msg,worker用户处理消息。

    为了简单起见,并且协程更适合单线程的方式,我们的主线程用来监听队列,子线程用于处理队列。这里使用redis的队列。主线程中有一个是无限循环,用户消费队列。

    1  whileTrue:
    2         task=rcon.rpop("queue")
    3         ifnottask:
    4             time.sleep(1)
    5             continue
    6         asyncio.run_coroutine_threadsafe(do_some_work(int(task)),new_loop)

    给队列添加一些数据:

    1 127.0.0.1:6379[3]>lpush queue2
    2 (integer)1
    3 127.0.0.1:6379[3]>lpush queue5
    4 (integer)1
    5 127.0.0.1:6379[3]>lpush queue1
    6 (integer)1
    7 127.0.0.1:6379[3]>lpush queue1

    可以看见输出:

    1 Waiting  2
    2 Done2
    3 Waiting  5
    4 Waiting  1
    5 Done1
    6 Waiting  1
    7 Done1
    8 Done5

    我们发起了一个耗时5s的操作,然后又发起了连个1s的操作,可以看见子线程并发的执行了这几个任务,其中5s awati的时候,相继执行了1s的两个任务。

    停止子线程

    如果一切正常,那么上面的例子很完美。可是,需要停止程序,直接ctrl+c,会抛出KeyboardInterrupt错误,我们修改一下主循环:

     1 try:
     2     whileTrue:
     3         task=rcon.rpop("queue")
     4         ifnottask:
     5             time.sleep(1)
     6             continue
     7         asyncio.run_coroutine_threadsafe(do_some_work(int(task)),new_loop)
     8 except KeyboardInterrupt ase:
     9     print(e)
    10     new_loop.stop()

    可是实际上并不好使,虽然主线程try了KeyboardInterrupt异常,但是子线程并没有退出,为了解决这个问题,可以设置子线程为守护线程,这样当主线程结束的时候,子线程也随机退出。

     1 new_loop=asyncio.new_event_loop()
     2 t=Thread(target=start_loop,args=(new_loop,))
     3 t.setDaemon(True)    # 设置子线程为守护线程
     4 t.start()
     5  
     6 try:
     7     whileTrue:
     8         # print('start rpop')
     9         task=rcon.rpop("queue")
    10         ifnottask:
    11             time.sleep(1)
    12             continue
    13         asyncio.run_coroutine_threadsafe(do_some_work(int(task)),new_loop)
    14 except KeyboardInterrupt ase:
    15     print(e)
    16     new_loop.stop()

    线程停止程序的时候,主线程退出后,子线程也随机退出才了,并且停止了子线程的协程任务。

    aiohttp

    在消费队列的时候,我们使用asyncio的sleep用于模拟耗时的io操作。以前有一个短信服务,需要在协程中请求远程的短信api,此时需要是需要使用aiohttp进行异步的http请求。大致代码如下:

    server.py

     1 importtime
     2 fromflask importFlask,request
     3  
     4 app=Flask(__name__)
     5  
     6 @app.route('/<int:x>')
     7 defindex(x):
     8     time.sleep(x)
     9     return"{} It works".format(x)
    10  
    11 @app.route('/error')
    12 deferror():
    13     time.sleep(3)
    14     return"error!"
    15  
    16 if__name__=='__main__':
    17     app.run(debug=True)

    /接口表示短信接口,/error表示请求/失败之后的报警。

    async-custoimer.py

     1 import time
     2 import asyncio
     3 from threading import Thread
     4 import redis
     5 import aiohttp
     6  
     7 def get_redis():
     8     connection_pool=redis.ConnectionPool(host='127.0.0.1',db=3)
     9     returnredis.Redis(connection_pool=connection_pool)
    10  
    11 rcon=get_redis()
    12  
    13 def start_loop(loop):
    14     asyncio.set_event_loop(loop)
    15     loop.run_forever()
    16  
    17 async def fetch(url):
    18     async with aiohttp.ClientSession()assession:
    19         async with session.get(url)asresp:
    20             print(resp.status)
    21             returnawait resp.text()
    22  
    23 async def do_some_work(x):
    24     print('Waiting ',x)
    25     try:
    26         ret=await fetch(url='http://127.0.0.1:5000/{}'.format(x))
    27         print(ret)
    28     except Exception ase:
    29         try:
    30             print(await fetch(url='http://127.0.0.1:5000/error'))
    31         except Exception ase:
    32             print(e)
    33     else:
    34         print('Done {}'.format(x))
    35  
    36 new_loop=asyncio.new_event_loop()
    37 t=Thread(target=start_loop,args=(new_loop,))
    38 t.setDaemon(True)
    39 t.start()
    40  
    41 try:
    42     whileTrue:
    43         task=rcon.rpop("queue")
    44         ifnottask:
    45             time.sleep(1)
    46             continue
    47         asyncio.run_coroutine_threadsafe(do_some_work(int(task)),new_loop)
    48 except Exception ase:
    49     print('error')
    50     new_loop.stop()
    51 finally:
    52     pass

    有一个问题需要注意,我们在fetch的时候try了异常,如果没有try这个异常,即使发生了异常,子线程的事件循环也不会退出。主线程也不会退出,暂时没找到办法可以把子线程的异常raise传播到主线程。(如果谁找到了比较好的方式,希望可以带带我)。

    对于redis的消费,还有一个block的方法:

    1 try:
    2     whileTrue:
    3         _,task=rcon.brpop("queue")
    4         asyncio.run_coroutine_threadsafe(do_some_work(int(task)),new_loop)
    5 except Exception ase:
    6     print('error',e)
    7     new_loop.stop()
    8 finally:
    9     pass

    使用 brpop方法,会block住task,如果主线程有消息,才会消费。测试了一下,似乎brpop的方式更适合这种队列消费的模型。

    1 127.0.0.1:6379[3]>lpush queue5
    2 (integer)1
    3 127.0.0.1:6379[3]>lpush queue1
    4 (integer)1
    5 127.0.0.1:6379[3]>lpush queue1

    可以看到结果

     1 Waiting  5
     2 Waiting  1
     3 Waiting  1
     4 200
     5 1It works
     6 Done1
     7 200
     8 1It works
     9 Done1
    10 200
    11 5It works
    12 Done5

    协程消费

    主线程用于监听队列,然后子线程的做事件循环的worker是一种方式。还有一种方式实现这种类似master-worker的方案。即把监听队列的无限循环逻辑一道协程中。程序初始化就创建若干个协程,实现类似并行的效果。

     1 import time
     2 import asyncio
     3 import redis
     4  
     5 now=lambda:time.time()
     6  
     7 def get_redis():
     8     connection_pool=redis.ConnectionPool(host='127.0.0.1',db=3)
     9     returnredis.Redis(connection_pool=connection_pool)
    10  
    11 rcon=get_redis()
    12  
    13 async def worker():
    14     print('Start worker')
    15  
    16     whileTrue:
    17         start=now()
    18         task=rcon.rpop("queue")
    19         ifnottask:
    20             await asyncio.sleep(1)
    21             continue
    22         print('Wait ',int(task))
    23         await asyncio.sleep(int(task))
    24         print('Done ',task,now()-start)
    25  
    26 def main():
    27     asyncio.ensure_future(worker())
    28     asyncio.ensure_future(worker())
    29  
    30     loop=asyncio.get_event_loop()
    31     try:
    32         loop.run_forever()
    33     except KeyboardInterrupt ase:
    34         print(asyncio.gather(*asyncio.Task.all_tasks()).cancel())
    35         loop.stop()
    36         loop.run_forever()
    37     finally:
    38         loop.close()
    39  
    40 if__name__=='__main__':
    41     main()

    这样做就可以多多启动几个worker来监听队列。一样可以到达效果。

    总结

    上述简单的介绍了asyncio的用法,主要是理解事件循环,协程和任务,future的关系。异步编程不同于常见的同步编程,设计程序的执行流的时候,需要特别的注意。比较这和以往的编码经验有点不一样。可是仔细想想,我们平时处事的时候,大脑会自然而然的实现异步协程。比如等待煮茶的时候,可以多写几行代码。

    转自:http://python.jobbole.com/87310/

     
  • 相关阅读:
    装箱拆箱操作
    《设计原本》试读:什么是设计
    代码大全中英文要点
    JSON对象和字符串之间的相互转换
    《设计原本》试读:何为真实?设计的概念
    Facebook后台技术探秘 世界最大的PHP网站
    重构模式:用异步计算替代长计算
    生命不息编程不止 Facebook CEO还在写代码
    万能的js复制按钮
    《网站设计解构》试读:1.1 可重用策略
  • 原文地址:https://www.cnblogs.com/zwgbk/p/10422759.html
Copyright © 2020-2023  润新知