1

我希望目前避免使用芹菜。在 Starlette 的文档中,他们提供了两种添加后台任务的方法:

通过石墨烯:https ://www.starlette.io/graphql/

class Query(graphene.ObjectType):
    user_agent = graphene.String()

    def resolve_user_agent(self, info):
        """
        Return the User-Agent of the incoming request.
        """
        user_agent = request.headers.get("User-Agent", "<unknown>")
        background = info.context["background"]
        background.add_task(log_user_agent, user_agent=user_agent)
        return user_agent

通过 JSON 响应:https ://www.starlette.io/background/

async def signup(request):
    data = await request.json()
    username = data['username']
    email = data['email']
    task = BackgroundTask(send_welcome_email, to_address=email)
    message = {'status': 'Signup successful'}
    return JSONResponse(message, background=task)

有谁知道用 Ariadne 在 Starlette 的背景中添加任务的方法?我无法在解析器中返回 JSONResponse,也无权访问 info.context["background"]。我唯一附加到我的上下文的是我的请求对象。

4

1 回答 1

2

解决了!

Starlette 中间件:

class BackgroundTaskMiddleware(BaseHTTPMiddleware):
    async def dispatch(
            self, request: Request, call_next: RequestResponseEndpoint
    ) -> Response:
        request.state.background = None
        response = await call_next(request)
        if request.state.background:
            response.background = request.state.background
        return response

Ariadne 解析器:

@query.field("getUser")
@check_authentication
async def resolve_get_user(user, obj, info):
    task = BackgroundTasks()
    task.add_task(test_func)
    task.add_task(testing_func_two, "I work now")
    request = info.context["request"]
    request.state.background = task
    return True


async def test_func():
    await asyncio.sleep(10)
    print("once!!")


async def testing_func_two(message: str):
    print(message)

这些函数仍然同步执行,但因为它们是后台任务,我不太担心。

更多讨论在这里。

于 2020-06-02T16:43:35.630 回答