情况
我有一台计算机连接到多个仪器。在这台计算机上,有一个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