跨多个进程的 Python threading.Condition() 功能

发布于 2024-12-10 20:31:13 字数 913 浏览 0 评论 0原文

我正在使用 mod_wsgi 编写 WSGI 应用程序。我想要有很多并发连接的能力。 mod_wsgi 为每个请求启动一个新进程,从而无法使用 threading.Condition() 来通知从一个线程到另一个线程的更改。

我知道有几种不同的方法可以在不同的运行进程(RPC、AMQP、d-bus、Jabber)之间提供实时消息传递,但我具体寻找的是接近单行 threading.wait( ) 和 threading.notifyAll()。

当我没有使用 mod_wsgi,而只是运行多个线程时,这基本上就是我为我工作的内容。显然,这两个函数是由不同的线程运行的:

def put_value:
    # user has given a new value
    # update in DB, then notify any waiting threads

    my_condition.acquire()
    my_condition.notifyAll()
    my_condition.release()

def get_value:
    # user has requested to receive a new value as of this point
    # we will return a value as soon as we are notified it has changed

    my_condition.acquire()
    my_condition.wait()
    my_condition.release()
    # return some val out of the DB

我再次寻找的是与单行 threading.wait() 和 threading.notifyAll() 接近的东西。我不介意设置一些配置,甚至在后台运行一些东西 - 但我有相当多的代码依赖于在请求中间停止和等待的能力,直到通知它可以继续。

I am writing a WSGI app using mod_wsgi. I want to have the ability of many concurrent connections. mod_wsgi spins up a new process for each request, making it impossible to use threading.Condition() for notifying a change from one thread to another thread.

I understand there is a few different ways to provide real time messaging between different running processes, (RPC, AMQP, d-bus, Jabber) but what I'm looking for specifically is something as close to the one-liners threading.wait() and threading.notifyAll().

When I wasn't using mod_wsgi, and just running multiple threads, here is essentially what I had working for me. Obviously the two functions were run by different threads:

def put_value:
    # user has given a new value
    # update in DB, then notify any waiting threads

    my_condition.acquire()
    my_condition.notifyAll()
    my_condition.release()

def get_value:
    # user has requested to receive a new value as of this point
    # we will return a value as soon as we are notified it has changed

    my_condition.acquire()
    my_condition.wait()
    my_condition.release()
    # return some val out of the DB

Once again, what I'm looking for is something as close to the one-liners threading.wait() and threading.notifyAll(). I don't mind setting up some configuration and even something running in the background - but I have a fair amount of code that relies on the ability to stop and wait in the middle of a request until it is notified that it may continue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

三生殊途 2024-12-17 20:31:13

在mod_wsgi配置中,设置processes=1。请参阅 http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives# WSGIDaemonProcess 了解详细信息。

In the mod_wsgi configuration, set processes=1. See http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess for details.

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