Python异步REST API,其响应依赖于CPU密集型计算.如何有效处理? [英] Python asynchronous REST API with responses which rely on CPU intensive calculations. How to handle efficiently?

查看:318
本文介绍了Python异步REST API,其响应依赖于CPU密集型计算.如何有效处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 aiohttp (一种简化版包含在下面,以说明我要解决的问题.

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.

API有两个端点-每个端点都调用一个执行一些计算的函数.两者之间的区别在于,对于其中一个端点,计算只需要10秒,而对于另一个端点,只需要1秒.

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.

我的代码在下面(实际计算已替换为time.sleep()调用).

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)

我想发生的事情:

如果我向较慢的端点发送请求,然后再向较快的端点发送请求,则我希望在较慢的计算仍在进行时首先从较快的端点接收响应.

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.

实际发生的情况:

由较慢的端点执行的计算正在阻塞.我在约10秒后收到来自慢速端点的响应,而在约11秒后收到来自快速端点的响应.

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.

我花了最后几个小时来回转,阅读了asynciomultiprocessing的内容,但是找不到任何可以解决我的问题的东西.也许我需要花一些时间来研究这个领域,以获得更好的理解,但是希望我能朝着正确的方向朝着期望的结果前进.

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.

推荐答案

在asyncio中应避免任何阻塞的IO调用.

Any blocking IO calls should be avoided in asyncio.

基本上time.sleep(10)会阻止整个aiohttp服务器10秒钟.

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

要解决此问题,请使用 loop.run_in_executor()调用:

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}

这篇关于Python异步REST API,其响应依赖于CPU密集型计算.如何有效处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆