简体   繁体   English

Flask 多处理在进程完成之前不会返回响应

[英]Flask multiprocessing does not return the response until a process is finished

I have a flask app with the code block:我有一个带有代码块的烧瓶应用程序:

if flag1 and flag2 and flag3:

        logger.info("Creating new process")
        p = Process(
            target=some_function,
            args=(
                param1,
                param2,
                param3,
                param4,
                param5
            ),
            daemon=False
        )
        p.start()
        logger.info("Return Now")
        return "OK", 200

Now, some_function takes a while to finish and I want to return the response to the client while it continues to do its work.现在, some_function 需要一段时间才能完成,我想在客户端继续工作时将响应返回给客户端。

I get to the point where it logs "Return Now" but it doesn't seem to return yet.我到了记录“立即返回”的地步,但它似乎还没有返回。 It seems like it's waiting for the process to finish before sending the response.似乎在发送响应之前等待进程完成。

I'm not an expert at linux or at python and flask but It works when I test it in a Windows machine using the normal flask run or python -m app commands.我不是linux或python和flask方面的专家,但是当我使用普通的flask run或python -m app命令在Windows机器上测试它时它可以工作。

As an additional information, I have it deployed in a linux server with uwsgi and it's executed with the command:作为附加信息,我将它部署在带有 uwsgi 的 linux 服务器中,并使用以下命令执行:

uwsgi --socket 127.0.0.1:9003 -w wsgi:app

Why could this be?为什么会这样?

Thanks in advance.提前致谢。

EDIT: I've also tried setting daemon to True but still the same result.编辑:我也试过将 daemon 设置为 True 但结果仍然相同。 Python version is 3.6 Python 版本是 3.6

EDIT: The application's process is handled by supervisord where it is executed by uwsgi and is behind nginx that is passed with uwsgi_pass, if it helps.编辑:应用程序的进程由 supervisord 处理,它由 uwsgi 执行,并在 nginx 后面,如果有帮助,它会通过 uwsgi_pass 传递。

EDIT: Thought I might want to add, I think this could be something with Nginx.编辑:我想我可能想补充一下,我认为这可能与 Nginx 相关。 As pointed out in the comments, it's highly unlikely that this is the flask application.正如评论中所指出的,这极不可能是烧瓶应用程序。 The thing on top now is Nginx.现在最重要的是 Nginx。

The real problem I'm trying to solve is: I'm sending a request to AWS API Gateway, which triggers a Lambda function that then makes the request to the API in a Linux server behind Nginx.我试图解决的真正问题是:我正在向 AWS API Gateway 发送一个请求,它触发一个 Lambda 函数,然后向 Nginx 后面的 Linux 服务器中的 API 发出请求。 The flask application works fine, except when it spawns the process, the control seems to not return.烧瓶应用程序工作正常,除非它产生进程时,控件似乎没有返回。 This happens when I call the API in Lambda, Postman or a python script.当我在 Lambda、Postman 或 Python 脚本中调用 API 时会发生这种情况。

I think what you're looking to do is detach the Process you're creating - in that case I would recommend trying the double-fork approach from this answer.我认为您要做的是分离您正在创建的流程 - 在这种情况下,我建议您尝试使用此答案中的双叉方法。

https://stackoverflow.com/a/49123627/6410635 https://stackoverflow.com/a/49123627/6410635

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM