python:async+await协程

1. asgiref

1.1写法

import asyncio
import datetime
from asgiref.sync import sync_to_async

def main(x):
    ss = 10000000 * x
    while x < ss:
        x += 1
    return x + 1

async def run(cc):
    print(f'开始-{datetime.datetime.now()}')
    await sync_to_async(main)(cc)
    print(f'结束-{datetime.datetime.now()}')


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    tasks = [run(i) for i in range(5, 10)]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()

output:
开始-2023-09-27 16:25:45.309243
开始-2023-09-27 16:25:45.326516
开始-2023-09-27 16:25:45.356943
开始-2023-09-27 16:25:45.386821
开始-2023-09-27 16:25:45.416780
结束-2023-09-27 16:25:52.762164
结束-2023-09-27 16:25:58.563407
结束-2023-09-27 16:26:05.763499
结束-2023-09-27 16:26:11.501453
结束-2023-09-27 16:26:16.968452

总时长:31s

1.2写法

import asyncio
import datetime
from asgiref.sync import sync_to_async
from concurrent.futures import ThreadPoolExecutor
_executor = ThreadPoolExecutor(8)


def main(x):
    ss = 10000000 * x
    while x < ss:
        x += 1
    return x + 1


async def run(cc):
    print(f'开始-{datetime.datetime.now()}')
    await sync_to_async(main)(cc)
    # await loop.run_in_executor(_executor, main, cc)
    print(f'结束-{datetime.datetime.now()}')


async def mm():
    tasks = []
    for i in range(5, 10):
        coroutine = run(i)
        task = asyncio.create_task(coroutine)
        tasks.append(task)
    await asyncio.wait(tasks)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    # tasks = [run(i) for i in range(5, 10)]
    # tasks = [asyncio.create_task(run(i)) for i in range(5, 10)]
    loop.run_until_complete(mm())
    loop.close()

output:
开始-2023-09-27 16:54:52.517194
开始-2023-09-27 16:54:52.538022
开始-2023-09-27 16:54:52.567939
开始-2023-09-27 16:54:52.597958
开始-2023-09-27 16:54:52.628119
结束-2023-09-27 16:54:56.829474
结束-2023-09-27 16:55:02.508599
结束-2023-09-27 16:55:12.366358
结束-2023-09-27 16:55:20.067500
结束-2023-09-27 16:55:27.610221

总时长:35s

2. 线程池

import asyncio
import datetime
from concurrent.futures import ThreadPoolExecutor
_executor = ThreadPoolExecutor(1)  # worker数量


def main(x):
    ss = 10000000 * x
    while x < ss:
        x += 1
    return x + 1


async def run(cc):
    print(f'开始-{datetime.datetime.now()}')
    await loop.run_in_executor(_executor, main, cc)
    print(f'结束-{datetime.datetime.now()}')


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    tasks = [run(i) for i in range(5, 10)]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()

output:
开始-2023-09-27 16:27:51.756005
开始-2023-09-27 16:27:51.768002
开始-2023-09-27 16:27:51.768002
开始-2023-09-27 16:27:51.797943
开始-2023-09-27 16:27:51.797943
结束-2023-09-27 16:27:56.600933
结束-2023-09-27 16:28:02.393268
结束-2023-09-27 16:28:07.087858
结束-2023-09-27 16:28:14.580390
结束-2023-09-27 16:28:23.332623

worker=1 总时长:32s
worker=4 总时长:27s
worker=8 总时长:25s

3. for循环

import datetime
def main(x):
    ss = 10000000 * x
    while x < ss:
        x += 1
    return x + 1


def run(cc):
    print(f'开始-{datetime.datetime.now()}')
    main(cc)
    print(f'结束-{datetime.datetime.now()}')


if __name__ == '__main__':
    tasks = [run(i) for i in range(5, 10)]

output:
开始-2023-09-27 16:32:32.953021
结束-2023-09-27 16:32:37.114201
开始-2023-09-27 16:32:37.114201
结束-2023-09-27 16:32:42.091429
开始-2023-09-27 16:32:42.091429
结束-2023-09-27 16:32:48.468582
开始-2023-09-27 16:32:48.468582
结束-2023-09-27 16:32:54.521932
开始-2023-09-27 16:32:54.521932
结束-2023-09-27 16:33:01.643154

总时长:29s

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值