简体   繁体   中英

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.

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.

As an additional information, I have it deployed in a linux server with uwsgi and it's executed with the command:

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. Python version is 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.

EDIT: Thought I might want to add, I think this could be something with Nginx. As pointed out in the comments, it's highly unlikely that this is the flask application. The thing on top now is 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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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