Python中的异步

目录

  • 什么是异步与异步的好处
  • 异步与多线程多进程
  • async , await,asyncio模块的使用
  • gevent 异步模块的使用

什么是异步与异步的好处Python中的异步_第1张图片

异步与多线程多进程

  • 轻量级的线程——协程
  • 可以获取异步函数的返回值
  • 主进程需要异步才行
  • 更适合文件读写使用

async与await关键字

  • async定义异步
  • await执行异步
  • 例子
async def test() :
	return 'a'
async def handle() :
	result = await test()

主程序如何执行异步函数呢?
if __name__=='__main_' :???

asyncio调用async函数

Python中的异步_第2张图片

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()是异步阻塞,只是对当前业务的阻塞。

gevent

  • pip install gevent
  • windows环境需要安装依赖:Microsoft Visual C++
  • 有些linux环境需要安装依赖:pip install wheel

gevent模块常用方法

Python中的异步_第3张图片

实战

#!/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

你可能感兴趣的:(Python全栈,python)