python3多进程包_一个异步多进程python包aiomultiprocess

aiomultiprocess是一个异步多进程的python库,其依赖于aiohttp和asyncio两个库。aiohttp是一个基于asyncio的异步http客户端和服务器。asyncio 是用来编写 并发 代码的库,使用 async/await 语法。asyncio 被用作多个提供高性能 Python 异步框架的基础,包括网络和网站服务,数据库连接库,分布式任务队列等等。

On their own, AsyncIO and multiprocessing are useful, but limited: AsyncIO still can’t exceed the speed of GIL, and multiprocessing only works on one task at a time. But together, they can fully realize their true potential.

aiomultiprocess presents a simple interface, while running a full AsyncIO event loop on each child process, enabling levels of concurrency never before seen in a Python application. Each child process can execute multiple coroutines(协程) at once, limited only by the workload and number of cores available.

举个例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29#coding:utf8

import asyncio

from aiohttp import request

from aiomultiprocess import Pool

url = "https://github.com"

async def request_one(json_req):

try:

print(json_req)

async with request(method='GET', url=url) as rsp:

return await rsp.text("utf-8"), json_req["uid"] #这里获取uid是为了区分返回的结果

except Exception:

print("error")

def request_batch(list_req):

async def run_all():

async with Pool(10) as pool:

#list传入请求列表,request_one单次请求函数

return await pool.map(request_one, list_req)

return asyncio.run(run_all()) #py3.7 asyncio.run() 函数用来运行最高层级的入口点 "main()" 函数

# loop = asyncio.get_event_loop()

# return loop.run_until_complete(run_all())

if __name__=="__main__":

out = request_batch([{"uid":"2"}, {"uid":"4"}])

print(len(out))

for o in out:

print(o)

asyncio.run is a Python 3.7 addition. In 3.5-3.6, your example is roughly equivalent to:

1

2

3

4

5import asyncio

futures = [...]

loop = asyncio.get_event_loop()

loop.run_until_complete(asyncio.wait(futures))

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值