-
python基础教程之Python3标准库:asyncio异步I/O、事件循环和并发工具(6)
本站最新发布 Python从入门到精通|Python基础教程
试听地址 https://www.xin3721.com/eschool/pythonxin3721/
asyncio
async def phase(i):
print('in phase {}'.format(i))
await asyncio.sleep(0.5 - (0.1 * i))
print('done with phase {}'.format(i))
return 'phase {} result'.format(i)
async def main(num_phases):
print('starting main')
phases = [
phase(i)
for i in range(num_phases)
]
print('waiting for phases to complete')
results = []
for next_to_complete in asyncio.as_completed(phases):
answer = await next_to_complete
print('received answer {!r}'.format(answer))
results.append(answer)
print('results: {!r}'.format(results))
return results
event_loop = asyncio.get_event_loop()
try:
event_loop.run_until_complete(main(3))
finally:
event_loop.close()
试听地址 https://www.xin3721.com/eschool/pythonxin3721/
asyncio
这个例子启动了多个后台阶段,它们会按其启动顺序的逆序完成。消费生成器时,循环会使用await等待协程的结果。
1.7 同步原语
尽管asyncio应用通常作为单线程的进程运行,不过仍被构建为并发应用。由于I/0以及其他外部事件的延迟和中断,每个协程或任务可能按一种不可预知的顺序执行。为了支持安全的并发执行,asyncio包含了threading和multiprocessing模块中一些底层原语的实现。
1.7.1 锁
Lock可以用来保护对一个共享资源的访问。只有锁的持有者可以使用这个资源。如果有多个请求要得到这个锁,那么其将会阻塞,以保证一次只有一个持有者。
- import asyncio
- import functools
- def unlock(lock):
- print('callback releasing lock')
- lock.release()
- async def coro1(lock):
- print('coro1 waiting for the lock')
- async with lock:
- print('coro1 acquired lock')
- print('coro1 released lock')
- async def coro2(lock):
- print('coro2 waiting for the lock')
- await lock.acquire()
- try:
- print('coro2 acquired lock')
- finally:
- print('coro2 released lock')
- lock.release()
- async def main(loop):
- # Create and acquire a shared lock.
- lock = asyncio.Lock()
- print('acquiring the lock before starting coroutines')
- await lock.acquire()
- print('lock acquired: {}'.format(lock.locked()))
- # Schedule a callback to unlock the lock.
- loop.call_later(0.1, functools.partial(unlock, lock))
- # Run the coroutines that want to use the lock.
- print('waiting for coroutines')
- await asyncio.wait([coro1(lock), coro2(lock)]),
- event_loop = asyncio.get_event_loop()
- try:
- event_loop.run_until_complete(main(event_loop))
- finally:
- event_loop.close()
锁可以直接调用,使用await来得到,并且使用结束时可以调用release()方法释放锁,如这个例子中的coro2()所示。还可以结合with await关键字使用锁作为异步上下文管理器,如coro1()中所示。
1.7.2 事件
asyncio.Event基于threading.Event。它允许多个消费者等待某个事件发生,而不必寻找一个特定值与通知关联。
- import asyncio
- import functools
- def set_event(event):
- print('setting event in callback')
- event.set()
- async def coro1(event):
- print('coro1 waiting for event')
- await event.wait()
- print('coro1 triggered')
- async def coro2(event):
- print('coro2 waiting for event')
- await event.wait()
- print('coro2 triggered')
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能