async def test() :
return 'a'
async def handle() :
result = await test()
主程序如何执行异步函数呢?
if __name__=='__main_' :???
async def main():
result = await asyncio.gather(
a(), # a和b是声明异步的函数
b()
)
print( result)
if __name__ == '__main__':
asyncio.run(main())
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/8/28 16:25
# @Author : InsaneLoafer
# @File : async_1.py
import os
import time
import random
import asyncio
async def a():
for i in range(10):
print(i, 'a', os.getpid())
await asyncio.sleep(random.random() * 2)
return 'a function'
async def b():
for i in range(10):
print(i, 'b', os.getpid())
await asyncio.sleep(random.random() * 2)
return 'b function'
async def main():
result = await asyncio.gather(
a(),
b()
)
print(result[0], result[1])
if __name__ == '__main__':
start = time.time()
asyncio.run(main())
print(time.time() - start)
print('parent pid is %s' % os.getpid())
0 a 42188
0 b 42188
1 a 42188
1 b 42188
2 a 42188
3 a 42188
4 a 42188
2 b 42188
5 a 42188
3 b 42188
4 b 42188
5 b 42188
6 a 42188
6 b 42188
7 b 42188
7 a 42188
8 b 42188
8 a 42188
9 a 42188
9 b 42188
a function b function
9.78639006614685
parent pid is 42188
Process finished with exit code 0
time.sleep()
是cpu级别的阻塞,而await asyncio.sleep()
与gevent.sleep()
是异步阻塞,只是对当前业务的阻塞。
pip install gevent
pip install wheel
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/8/28 16:25
# @Author : InsaneLoafer
# @File : async_1.py
import os
import time
import random
import asyncio
import gevent
def gevent_a():
for i in range(5):
print(i, 'gevent_a', os.getpid())
gevent.sleep(random.random() * 2)
return 'gevent_a result'
def gevent_b():
for i in range(5):
print(i, 'gevent_b', os.getpid())
gevent.sleep(random.random() * 2)
return 'gevent_b result'
async def a():
for i in range(10):
print(i, 'a', os.getpid())
await asyncio.sleep(random.random() * 2)
return 'a function'
async def b():
for i in range(10):
print(i, 'b', os.getpid())
await asyncio.sleep(random.random() * 2)
return 'b function'
async def main():
result = await asyncio.gather(
a(),
b()
)
print(result[0], result[1])
if __name__ == '__main__':
start = time.time()
# asyncio.run(main())
g_a = gevent.spawn(gevent_a)
g_b = gevent.spawn(gevent_b)
gevent_list = [g_a, g_b]
result = gevent.joinall(gevent_list)
print(result[0].value)
print(result[1].value)
print(time.time() - start)
print('parent pid is %s' % os.getpid())
0 gevent_a 14832
0 gevent_b 14832
1 gevent_b 14832
1 gevent_a 14832
2 gevent_b 14832
2 gevent_a 14832
3 gevent_b 14832
4 gevent_b 14832
3 gevent_a 14832
4 gevent_a 14832
gevent_b result
gevent_a result
6.099842071533203
parent pid is 14832
Process finished with exit code 0