首页 > 解决方案 > uwsgi/nginx 在子进程完成之前不响应

问题描述

我有带有 nginx 的服务器,将 http 流量传递给运行我的 django 应用程序的 uwsgi。我正在调用的 django 视图正在创建一个运行以下 test.sh bash 脚本的子进程:

#!/bin/sh
echo hello
sleep 10
echo morning

出于某种原因,在 bash 完成之前,我没有从视图中得到 200 响应,即打印“早上”,尽管子进程应该创建一个并行运行的线程。

在尝试调试此问题时,我注意到如果我在直接使用 djangopython manage.py runserver或使用 uwsgi 运行时从服务器本地 cURL,http=127.0.0.1:8000我会在预期的“早上”打印之前得到 200 响应。

任何想法为什么使用 socket=127.0.0.1:8000 和 nginx 它的行为不同?

我的 nginx 配置为将 http 流量传递给 django:

upstream django {
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}

server {
    listen 80;

    location /api/ {
            uwsgi_pass  django;
            include     /path_to/uwsgi_params; # the uwsgi_params file you installed
    }
}

我的uwsgi ini:

[uwsgi]
socket=127.0.0.1:8000
module=my_module.wsgi:application
chdir=/project_dir
master=true
vacuum=true
logger=file:/log_path
stats=/tmp/uwsgi_stats.socket
single interpreter=true

我的 view.py 是:

from subprocess import Popen
from rest_framework import views
from rest_framework.response import Response

class TestApi(views.APIView):
    def post(self, request, *args, **kwargs):
        Popen(["/opt/test.sh"])
        return Response({})

标签: pythondjangonginxsubprocessuwsgi

解决方案


推荐阅读