当使用multiprocessing.Process而不是subprocess.Popen生成子流程时,uWSGI服务器将阻塞

时间:2019-07-06 22:04:59

标签: python subprocess uwsgi

我的uWSGI应用程序需要产生一个长期运行的Python任务,因此我正在使用multiprocessing。但是,当我启动此子进程时,服务器不会在子进程完成之前关闭套接字。

为帮助调试,我还使用Popen生成了一个子进程,并且服务器等待popen进程完成。

我已根据this answer启用了close-on-exec,而multiprocessing.Process步骤仍然阻止了服务器。

示例服务器代码:

import multiprocessing, subprocess
def _helper(url):
    import time
    time.sleep(10)

def download(url):
    # server closes socket if this is still running, as expected
    p = subprocess.Popen(["/bin/sleep", "1000"])

    # server does not close socket until this finishes
    multiprocessing.Process(target=_helper, args=(url,)).start()

def application(env, start_response):
    download("sample_url")
    print("download() returned!")
    start_response("200 OK", [("Content-Type", "text/html")])
    return [b"ok"]

我从uwsgi --socket 127.0.0.1:9090 --wsgi-file server.py --close-on-exec启动服务器,并在Python 3.6.8中使用uWSGI 2.0.18。

0 个答案:

没有答案
相关问题