python 中的 Web 服务器脚本 - 维护状态

web server scripting in python - maintaining state

我正在 python 中基于 nginx + uWSGI 和 运行ning cgi 脚本构建一个小型网络服务器,但是(可能是因为我也在使用 pyro)执行时间是超过 1/2 秒(在 Raspberry pi 上)。我需要 运行 python 代码,以便它在请求之间保持状态,这应该会显着提高性能。我该怎么做?

我怀疑答案需要类似于 运行ning python 所以它会循环,将请求从队列中拉出,但我不知道如何做到这一点。

这是简单的脚本 - 执行大约 600 毫秒,pyro 远程 class 的另一端在同一台机器上。

#!/usr/bin/env python
import Pyro4
import logging

import cgi
import cgitb

cgitb.enable()

camuri = "PYRONAME:camerapi:cameraserv"`
camlist = Pyro4.Proxy(camuri)

print "Content-type: text/html\n\n"
clist = camlist.cameraList()

print("<h3>List of cameras</h3>")

for i in clist:
    acam = clist[0]
    print("<p>Camera: %d %s</p>" % (acam['ind'], acam['name']))

问题是您使用的是 CGI。没有理由这样做。

您想要的行为正是 WSGI 的工作方式。你应该使用它。