在 mod_wsgi 上的 web.py 应用程序中保持并发

发布于 2024-10-16 11:27:54 字数 773 浏览 3 评论 0原文

抱歉,如果这没有意义。如果需要澄清,请发表评论。

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀中猫帐中妖 2024-10-23 11:27:54

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.

幽蝶幻影 2024-10-23 11:27:54

来自 PEP 333,定义 WSGI:

可以并行运行多个请求的服务器还应该提供以单线程方式运行应用程序的选项,以便非线程安全的应用程序或框架仍然可以与该服务器一起使用

检查 WSGI 服务器的文档。

From PEP 333, defining WSGI:

Servers that can run multiple requests in parallel, should also provide the option of running an application in a single-threaded fashion, so that applications or frameworks that are not thread-safe may still be used with that server

Check the documentation of your WSGI server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文