0

情况

我有一台计算机连接到多个仪器。在这台计算机上,有一个nginx服务器使用uWSGI为Falcon WSGI应用程序提供服务。该应用程序被认为是,如果一个用户需要访问仪器,其他人将无法使用它。我通过以下一段(我的精简版)代码实现了这一点:

import json
import falcon

class Lab(object):
    def __init__(self):
        self.available_instruments = ["money_maker", "unicornifier"]  # Not actual instruments
        self.connected_instruments = []
    def on_get(self, request, response):
        response.status = falcon.HTTP_OK
        response.content_type = "application/json"
        response.body = json.dumps({
            "connected": self.connected_instruments,
            "available": self.available_instruments
        })
    def on_post(self, request, response):
        json_body = json.loads(request.body)
        instrument = json_body['connect']
        if instrument in self.connected_instruments:
            raise falcon.HTTPBadRequest('Busy')
        elif instrument not in self.available_instruments:
            raise falcon.HTTPBadRequest('No such instrument')
        self.connected_instruments.append(instrument)
        response.status = falcon.HTTP_OK
        response.content_type = "application/json"
        response.body = json.dumps({
            "connected": self.connected_instruments,
            "available": self.available_instruments
        })

application = falcon.API()
l = Lab()
application.add_route('/', lab)

和请求正文

{
    "connect": "money_maker"
}

问题

当我“连接”一个仪器时,立即的答案显示它已连接。但是连续的 GET 请求并没有给出预期的答案。我明白了

{
    "connected": [],
    "available": ["money_maker", "unicornifier"]
}

但是,如果我出于测试目的在本地 uWSGI 实例上执行上述代码,则不会发生这种情况。是否有任何我不知道的 nginx-uWSGI 交互?感谢您提供任何帮助。

为了完整起见,这里跟随uWSGI调用nginx.conf的文件。api.ini

nginx/站点可用/api

#nginx config file for uWSGI requests routing
server {
    listen 80;
    server_name example.com;
    location /api {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/api.sock;
    }
}

api.ini

[uwsgi]

master = true
processes = 5

socket = /tmp/%n.sock
chmod-socket = 666
uid = apidev
gid = www-data

chdir = %d../%n
pythonpath = %d../%n

module = %n

vacuum = true
4

1 回答 1

0
self.connected_instruments.append(instrument)

只是将发布的数据存储在内存中!即,在一个进程的内存空间中......但是当您在 nginx 后面运行5 个uwsgi 进程时,您很有可能将数据发布到一个进程,然后由另一个没有该数据的进程提供服务。

您必须使用数据库或所有进程都可以共享的东西来保存和检索数据。

于 2016-05-25T17:42:54.547 回答