在 mod_wsgi 上的 web.py 应用程序中保持并发
抱歉,如果这没有意义。如果需要澄清,请发表评论。
我正在 web.py 中编写一个小文件上传应用程序,并使用 mod_wsgi + apache 进行部署。我的会话管理一直存在问题,希望澄清 web.py 中的线程如何工作。
本质上,我在有人访问我的页面时呈现的 html 页面的隐藏字段中嵌入了代码。然后通过包含文件和代码的标准 POST 请求完成文件上传。然后,我通过在文件上传 POST 方法中更新文件并通过对不同类的 GET 请求获取文件来检索文件的进度。 “会话”(抱歉它相当幼稚)存储在这样的会话对象中:
class session:
def __init__(self):
self.progress = 0
self.title = ""
self.finished = False
def advance(self):
self.progress = self.progress + 1
会话全部保存在我的应用程序脚本内的全局字典中,然后使用我的代码(之前的代码)作为密钥进行访问。
由于某种原因,我的进度似乎停留在 0 并且永远不会增加。我现在已经调试了几个小时,发现上传类和进度类引用的两个会话对象不一样。然而,这两个代码(据我所知)是相等的。这让我很生气,因为它在我本地计算机上的 web.py 测试服务器上运行没有任何问题。
编辑:经过一些研究,似乎字典可能会针对每个请求进行复制。我尝试将字典放入另一个字典中并导入,但这不起作用。除了使用数据库“分离”会话字典之外,还有其他方法吗?
Sorry if this makes no sense. Please comment if clarification is needed.
I'm writing a small file upload app in web.py which I am deploying using mod_wsgi + apache. I have been having a problem with my session management and would like clarification on how the threading works in web.py.
Essentially I embed a code in a hidden field of the html page I render when someone accesses my page. The file upload is then done via a standard POST request containing both the file and the code. Then I retrieve the progress of the file by updating it in the file upload POST method and grabbing it with a GET request to a different class. The 'session' (apologies for it being fairly naive) is stored in a session object like this:
class session:
def __init__(self):
self.progress = 0
self.title = ""
self.finished = False
def advance(self):
self.progress = self.progress + 1
The sessions are all kept in a global dictionary within my app script and then accessed with my code (from earlier) as the key.
For some reason my progress seems to stay at 0 and never increments. I've been debugging for a couple hours now and I've found that the two session objects referenced from the upload class and the progress class are not the same. The two codes, however, are (as far as I can tell) equal. This is driving me mad as it worked without any problems on the web.py test server on my local machine.
EDIT: After some research it seems that the dictionary may get copied for every request. I've tried putting the dictionary in another and importing but this doesn't work. Is there some other way short of using a database to 'seperate' the sessions dictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Apache/mod_wsgi 可以在多进程配置中运行,并且可能您的请求甚至不会由同一进程提供服务,并且如果对于该多进程配置,每个进程都是单线程,则永远不会,因为在上传时,同一进程无法处理其他请求过程。阅读:
http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading
可能您应该使用 mod_wsgi 守护进程模式和单个多线程守护进程。
Apache/mod_wsgi can run in multiprocess configurations and possible your requests aren't even being serviced by the same process and never will if for that multiprocess configuration each process is single thread because while the upload is occuring no other requests can be handled by that same process. Read:
http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading
Possibly you should use mod_wsgi daemon mode with single multiple thread daemon process.
来自 PEP 333,定义 WSGI:
检查 WSGI 服务器的文档。
From PEP 333, defining WSGI:
Check the documentation of your WSGI server.