pythonapi异步,Python异步REST API,其响应依赖于CPU密集型计算。如何有效处理?

I have written a basic REST API using aiohttp, a simplified version of which is included below to illustrate the problem I am looking to solve.

The API has two endpoints - each of which calls a function that performs some calculations. The difference between the two is that for one of the endpoints, the calculations take 10 seconds, and for the other they take only 1 second.

My code is below (the actual calculations have been replaced with time.sleep() calls).

import time

from aiohttp import web

def simple_calcs():

time.sleep(1) # Pretend this is the simple calculations

return {'test': 123}

def complex_calcs():

time.sleep(10) # Pretend this is the complex calculations

return {'test': 456}

routes = web.RouteTableDef()

@routes.get('/simple_calcs')

async def simple_calcs_handler(request):

results = simple_calcs()

return web.json_response(results)

@routes.get('/complex_calcs')

async def complex_calcs_handler(request):

results = complex_calcs()

return web.json_response(results)

app = web.Application()

app.add_routes(routes)

web.run_app(app)

What I would like to happen:

If I send a request to the slower endpoint, then immediately afterwards send a request to the faster endpoint, I would like to receive a response from the faster endpoint first while the slower calculations are still ongoing.

What actually happens:

The calculations being carried out by the slower endpoint are blocking. I receive the response from the slow endpoint after ~10 seconds and from the fast endpoint after ~11 seconds.

I've spent the last few hours going round in circles, reading up on asyncio and multiprocessing, but unable to find anything that could solve my problem. Probably I need to spend a bit longer studying this area to gain a better understanding, but hoping I can get a push in the right direction towards the desired outcome.

解决方案

Any blocking IO calls should be avoided in asyncio.

Essentially time.sleep(10) blocks the whole aiohttp server for 10 seconds.

To solve it please use loop.run_in_executor() call:

async def complex_calcs():

loop = asyncio.get_event_loop()

loop.run_in_executor(None, time.sleep, 10) # Pretend this is the complex calculations

return {'test': 456}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值