当前位置: 首页 > 知识库问答 >
问题:

使用mongodb的Flask/uwsgi/nginx应用程序超时

松英喆
2023-03-14
from flask import Flask
from flask_cors import CORS
from bson.json_util import dumps
import pymongo

DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
CORS(app)

client = pymongo.MongoClient() # This line
db = client.myapp

@app.route('/api/test')
def test():
    item = db.items.find_one()
    return item['name']

def create_app(app_name='MYAPP'):
    return app

# if __name__ == '__main__':
#   app.run(debug=True, threaded=True, host='0.0.0.0')
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name example.com www.example.com;

    location /api {
        include uwsgi_params;
        uwsgi_read_timeout 3600;
        uwsgi_pass unix:/var/www/html/myapp/myapp.sock;
    }

    location / {
      root /var/www/html/myapp;
      try_files $uri $uri/ /index.html;
    }

    #return 301 https://$server_name$request_uri;
}
[uwsgi]
module = wsgi:app

master = true
processes = 4
enable-threads = True

socket = /var/www/html/myapp/myapp.sock
chmod-socket = 660
vacuum = true

die-on-term = true
[Unit]
Description=uWSGI Python container server
After=network.target

[Service]
User=pi
Group=www-data
WorkingDirectory=/var/www/html/myapp
ExecStart=/usr/bin/uwsgi --ini /etc/uwsgi/apps-available/myapp.ini

[Install]
WantedBy=multi-user.target

共有1个答案

呼延俊风
2023-03-14

我认为问题是你在分叉,这导致了Pymongo的问题。

PyMongo是线程安全的,但不是分叉安全的。一旦在守护进程模式下运行应用程序,您就会分叉进程。您必须在应用程序中创建一个MongoClient,以便您的线程可以在进程启动后看到它。

您可以试试这个(我没有尝试过这个,我通常在类中包装类似的东西,并在init方法中这样做):

def create_app(app_name='MYAPP'):
   app.client = pymongo.MongoClient(connect=False) # this will prevent connecting until you need it.
   app.db = app.client.myapp
   return app
 类似资料:
  • 我的应用程序有以下堆栈: Nginx(1.4.6) UWSGI(1.9.17.1-Debian(64bit)) 烧瓶 Python 3.4 NGINX重启后的问题在一段时间内(几分钟)都能正常工作。在此期间之后,我收到一个“504网关超时”错误。 NGINX日志: *13从上游读取响应标头时上游超时(110:连接超时),客户端:86.123.39.44,服务器:app.fc.com,请求:“get

  • 我正在运行一个带有和的Python web服务器,基映像为。 我想传递我的客户端证书DN,如果一个存在。为此,我定义了以下: 我想让我的Flask代码接收参数,但找不到如何接收。 环顾四周,我发现它应该驻留在的对象中,但是在打印环境内容时,我没有看到任何这样的键。 在通过邮递员发送请求时,返回以下内容以供参考: dict_keys.input“、”wsgi.file_wrapper“、”wsgi.

  • Supported tags and respective Dockerfile links python3.9, latest (Dockerfile) python3.8, (Dockerfile) python3.7, (Dockerfile) python3.6 (Dockerfile) Discouraged tags python3.8-alpine (Dockerfile) To l

  • 我试图设置NGINX,uWSGI和烧瓶。我目前正在得到, uWSGI错误 找不到Python应用程序 我得到了一些奇怪的错误在我的uwsgi错误文件,你可以在我的文章底部找到。 我会直截了当地说,这是在运行Ubuntu 13.04 64位的新VPS上,这些是我运行的命令。 sudo apt-get更新 sudo apt-get安装构建基本 sudo apt-get安装python-dev sudo

  • 很好用。但是,如果我发布了一个更大的json文件,我会得到一个504:Gateway超时。 我感觉Nginx和Uwsgi之间的通信有一个问题,但我不知道如何解决它。 编辑:我跳入docker容器并手动重启nginx以获得更好的日志记录。我收到以下错误: /etc/nginx/conf.d/nginx.conf: /etc/nginx/conf.d/upload.conf:

  • 问题内容: 我打算使用Kubernetes和Ingress进行负载平衡。我正在尝试学习如何设置Flask,uWSGI和Nginx。我看到本教程将所有三个安装在同一容器中,我想知道是否应该使用它。 https://ianlondon.github.io/blog/deploy-flask-docker- nginx/ 我猜测将它们作为单独的容器和单独的容器的好处是它们可以分别进行单独缩放? 但是,F