VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > python教程 >
  • python基础教程之Python3标准库:asyncio异步I/O、事件循环和并发工具(7)

本站最新发布   Python从入门到精通|Python基础教程
试听地址  
https://www.xin3721.com/eschool/pythonxin3721/


  •  
  • async def main(loop):
  • # Create a shared event
  • event = asyncio.Event()
  • print('event start state: {}'.format(event.is_set()))
  •  
  • loop.call_later(
  • 0.1, functools.partial(set_event, event)
  • )
  •  
  • await asyncio.wait([coro1(event), coro2(event)])
  • print('event end state: {}'.format(event.is_set()))
  •  
  • event_loop = asyncio.get_event_loop()
  • try:
  • event_loop.run_until_complete(main(event_loop))
  • finally:
  • event_loop.close()
  • 与Lock一样,coro1()和coro2()会等待设置事件。区别是一旦事件状态改变,它们便可以立即启动,并且它们不需要得到事件对象上的唯一的锁。

    1.7.3 条件

    Condition的做法与Event类似,只不过不是通知所有等待的协程,被唤醒的等待协程的数目由notify()的一个参数控制。 

    
    
    1. import asyncio
    2.  
    3. async def consumer(condition, n):
    4. async with condition:
    5. print('consumer {} is waiting'.format(n))
    6. await condition.wait()
    7. print('consumer {} triggered'.format(n))
    8. print('ending consumer {}'.format(n))
    9.  
    10. async def manipulate_condition(condition):
    11. print('starting manipulate_condition')
    12.  
    13. # pause to let consumers start
    14. await asyncio.sleep(0.1)
    15.  
    16. for i in range(1, 3):
    17. async with condition:
    18. print('notifying {} consumers'.format(i))
    19. condition.notify(n=i)
    20. await asyncio.sleep(0.1)
    21.  
    22. async with condition:
    23. print('notifying remaining consumers')
    24. condition.notify_all()
    25.  
    26. print('ending manipulate_condition')
    27.  
    28. async def main(loop):
    29. # Create a condition
    30. condition = asyncio.Condition()
    31.  
    32. # Set up tasks watching the condition
    33. consumers = [
    34. consumer(condition, i)
    35. for i in range(5)
    36. ]
    37.  
    38. # Schedule a task to manipulate the condition variable
    39. loop.create_task(manipulate_condition(condition))
    40.  
    41. # Wait for the consumers to be done
    42. await asyncio.wait(consumers)
    43.  
    44. event_loop = asyncio.get_event_loop()
    45. try:
    46. result = event_loop.run_until_complete(main(event_loop))
    47. finally:
    48. event_loop.close()

    这个例子启动Condition的5个消费者。它们分别使用wait()方法来等待通知让它继续。manipulate_condition()通知一个消费者,再通知两个消费者,然后通知所有其余的消费者。

    1.7.4 队列

    asyncio.Queue为协程提供了一个先进先出的数据结构,这与线程的queue.Queue或进程的multiprocessing.Queue很类似。

    
    
    1. import asyncio
    2.  
    3. async def consumer(n, q):
    4. print('consumer {}: starting'.format(n))
    5. while True:
    6. print('consumer {}: waiting for item'.format(n))
    7. item = await q.get()
    8. print('consumer {}: has item {}'.format(n, item))
    9. if item is None:
    10. # None is the signal to stop.
    11. q.task_done()
    12. break
    13. else:
    14. await asyncio.sleep(0.01 * item)
    15. q.task_done()
    16. print('consumer {}: ending'.format(n))
    17.  
    18. async def producer(q, num_workers):
    19. print('producer: starting')
    20. # Add some numbers to the queue to simulate jobs
    21. for i in range(num_workers * 3):
    22. await q.put(i)
    23. print('producer: added task {} to the queue'.format
    相关教程