2

这似乎是可能的,因为app.Sanic.handle_request()有这个片段

            if isawaitable(response):
                response = await response

这就是awaitablePython 的检查方式:

def isawaitable(object):
    """Return true if object can be passed to an ``await`` expression."""
    return (isinstance(object, types.CoroutineType) or
            isinstance(object, types.GeneratorType) and
                bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
            isinstance(object, collections.abc.Awaitable))

我知道用来async def创建一个可等待的函数,但我不知道如何创建一个可等待的HTTPResponse实例。如果可能的话,用一个简单的方法来查看一个可等待响应的示例真的很有帮助await asyncio.sleep(5)


尝试了 Mikhail 的解决方案,这是我观察到的:

  • raise500进入asyncio.sleep()
  • ret500不进asyncio.sleep()(bug)
  • raise500阻止其他raise500(错误)
  • raise500不阻塞ret500
  • 无法判断是否ret500会阻止其他ret500,因为它太快了(不睡觉)

完整代码(通过另存为 运行test.py,然后在 shell 中python test.py并转到http://127.0.0.1:8000/api/test):

import asyncio
from sanic import Sanic
from sanic.response import HTTPResponse
from sanic.handlers import ErrorHandler


class AsyncHTTPResponse(HTTPResponse):  # make it awaitable
    def __await__(self):
        return self._coro().__await__()  # see https://stackoverflow.com/a/33420721/1113207

    async def _coro(self):
        print('Sleeping')
        await asyncio.sleep(5)
        print('Slept 5 seconds')
        return self


class CustomErrorHandler(ErrorHandler):
    def response(self, request, exception):
        return AsyncHTTPResponse(status=500)


app = Sanic(__name__, error_handler=CustomErrorHandler())


@app.get("/api/test")
async def test(request):
    return HTTPResponse(status=204)


@app.get("/api/raise500")
async def raise500(request):
    raise Exception


@app.get("/api/ret500")
async def ret500(request):
    return AsyncHTTPResponse(status=500)

if __name__ == "__main__":
    app.run()
4

2 回答 2

3

实现__await__魔术方法的类变得可等待。

我没有检查它是否适用于您的情况,但这里是创建等待自定义类实例的示例:

import asyncio
from inspect import isawaitable


class HTTPResponse:  # class we have
    pass


class AsyncHTTPResponse(HTTPResponse):  # make it awaitable
    def __await__(self):
        return self._coro().__await__()  # see https://stackoverflow.com/a/33420721/1113207

    async def _coro(self):
        await asyncio.sleep(2)
        return self


async def main():
    resp = AsyncHTTPResponse()

    if isinstance(resp, HTTPResponse):
        print('It is HTTPResponse class ...')

    if isawaitable(resp):
        print('... which is also awaitable.')

    print('Let us see how it works.')
    await resp


loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main())
finally:
    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.close()
于 2017-11-24T16:42:07.117 回答
3

由于米哈伊尔的答案是正确的,我将只讨论进一步的编辑

  • raise500 阻止其他 raise500 (bug)

它似乎没有阻塞。一个简单的测试(添加了一些查询字符串来区分请求):

for i in `seq 2`;do curl http://127.0.0.1:8000/api/raise500&req=$i & done

从日志的日期时间来看,请求之间没有延迟(阻塞)

Sleeping
Sleeping
Slept 5 seconds
2017-11-26 01:01:49 - (network)[INFO][127.0.0.1:37310]: GET http://127.0.0.1:8000/api/raise500?req=1  500 0
Slept 5 seconds
2017-11-26 01:01:49 - (network)[INFO][127.0.0.1:37308]: GET http://127.0.0.1:8000/api/raise500?req=2  500 0
  • ret500 不进入 asyncio.sleep() (bug)

这是因为您在 awaitable 函数中返回 awaitable ,但 sanic await 仅适用于第一个:

@app.get("/api/ret500")
async def ret500(request):
    return AsyncHTTPResponse(status=500)

这样handle_request做:

response = ret500(request)  # call `async def ret500` and returns awaitable
if isawaitable(response):
    response = await response  # resolve and returns another awaitable - AsyncHTTPResponse object

# note to wait 5 seconds sanic would need again await for it
# response = await response

解决方案:

  1. 不要返回等待,换句话说await AsyncHTTPResponse,你自己

    @app.get("/api/ret500")
    async def ret500(request):
        res = await AsyncHTTPResponse(status=500)
        return res
    
  2. 放弃ret500async

    @app.get("/api/ret500")
    def ret500(request):
        return AsyncHTTPResponse(status=500)
    

    注意:此技术仅在不打算在其中调用异步函数时才有效。

于 2017-11-26T00:29:28.840 回答