当前位置:网站首页>Using asyncio for concurrency
Using asyncio for concurrency
2022-07-01 01:05:00 【Geek style】
Concurrency and parallelism have always been confusing concepts . Concurrency usually means that multiple tasks need to be performed at the same time , In parallel, multiple tasks are executed at the same time .
asyncio Implement concurrency , It's going to take more than one process to complete the task , Whenever there's a task jam, it's await, And then the other coroners continue to work . Create a list of multiple collaborations , Then register these processes in the time cycle .
One 、asyncio Implement concurrency
import asyncio
import time
now = lambda:time.time()
def events_demo():
async def do_some_work(x):
print('Waiting:',x)
await asyncio.sleep(x)
return 'Done after {}s'.format(x)
start = now()
# Create multiple collaboration objects
coroutine1=do_some_work(1)
coroutine1=do_some_work(2)
coroutine1=do_some_work(4)
# Create a task list
tasks=[
asyncio.ensure_future(coroutine1),
asyncio.ensure_future(coroutine2),
asyncio.ensure_future(coroutine3)
]
# Register the task list into the event loop
loop=asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
# Get the returned result
for task in tasks:
print('Task ret:',task.result())
print('TIME:',now()-start)
Two 、 Summary of the experiment
Running result display , In parallel, it should be 1+2+4=7 second ; Execute each task concurrently do_some_work, The first time it took was 1 second , It takes time to execute other cooperation processes , The second time is 2 second , It takes time to execute other cooperation processes , And so on . In case of time-consuming blocking, other processes are performed , The longest time is 4 second , Three tasks 4 Seconds to complete .
边栏推荐
- Oracle data integrity
- Oracle table creation and management
- The question of IBL precomputation is finally solved
- CSDN常用复杂公式模板记录
- 【2023联发科提前批笔试题】~ 题目及参考答案
- Redis based distributed lock
- 2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report
- leetcode 474. Ones and zeroes (medium)
- New content violation degree determination scana bad information monitoring capability update issue 5
- A letter to 5000 fans!
猜你喜欢

2022-2028 global ethylene oxide scrubber industry research and trend analysis report

2022-2028 global weight loss ginger tea industry research and trend analysis report

PyTorch安装并使用gpu加速

C#生成putty格式的ppk文件(支持passphrase)

20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract

2022-2028 global PTFE lined valve industry research and trend analysis report

2022-2028 global retro glass industry research and trend analysis report

CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解

New content violation degree determination scana bad information monitoring capability update issue 5

20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
随机推荐
Can JDBC based on openjdk connect to MySQL?
C language file operation for conquering C language
left join左连接匹配数据为NULL时显示指定值
Authentication principle of Ranger plug-in
20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
Unit test concept and purpose
JS bubble sort and select sort
2022-2028 global weight loss ginger tea industry research and trend analysis report
解决 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
Ybtoj exchange game [tree chain splitting, line segment tree merging]
连表查询 select 生成
剑指 Offer 19. 正则表达式匹配
Sword finger offer 19 Regular Expression Matching
2022-2028 global rampant travel industry research and trend analysis report
20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
2022-2028 global public address fire alarm system industry research and trend analysis report
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br
$watch will not trigger data change - $watch not firing on data change