python3.7 contextvars在asyncio使用的
python3.7 asyncio 使用
2023-09-14 09:06:36 时间
from contextvars import ContextVar
import asyncio
import random
cv = ContextVar('cv')
async def waiting_func(name):
print(f'{name} Before sleep: {cv.get()}')
await asyncio.sleep(random.random())
print(f'{name} After 1 sleep: {cv.get()}')
await asyncio.sleep(random.random())
print(f'{name} After 2 sleep: {cv.get()}')
async def task(name):
await waiting_func(name)
async def main():
for name in ('first', 'second', 'third'):
cvt = cv.set(name)
await task(name)
cv.reset(cvt)
if __name__ == '__main__':
asyncio.run(main())
相关文章
- python3.7安装pip_centos怎么安装
- python3.7如何安装numpy库_python升级后第三方库
- ubuntu安装python3.7,并更新python默认指向为python3.7
- python3.7-MongoDB-'Collection' object is not callable.
- python3.7错误ImportError: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found
- python3.7错误ImportError: /lib64/libc.so.6: version `GLIBC_2.18' not found
- centos7安装python3.7_python软件安装步骤
- 使用 centerOS 7 部署 django 项目 python3.7.3
- Linux 安装python3.7.6
- Centos7安装python3.7
- python3.7 安装alipay-sdk-python(PyCrypto)
- Python web实战之Python3.7+ Django+百度地图API【配套视频】
- 初识Python3.7的dataclasses标准库
- CentOS6.5安装python3.7详细步骤
- CentOS7安装Python3.7详解编程语言