如何从 Twisted 中推迟 Django DB 操作?

发布于 2024-08-08 20:35:42 字数 972 浏览 5 评论 0原文

我有一个正常的 Django 站点正在运行。此外,还有另一个扭曲的进程,它监听 Jabber 存在通知并使用 Django 的 ORM 更新 Django DB。

到目前为止,它的工作原理是我只是调用相应的 Django 模型(在正确设置设置环境之后)。然而,这会阻止 Twisted 应用程序,这不是我想要的。

由于我是扭曲的新手,我不知道最好的方法是使用延迟以非阻塞方式访问 Django DB(通过其 ORM)。

  1. 延迟生成器?
  2. 扭曲的企业.adbapi? (绕过 ORM?)
  3. ???

如果解析了存在消息,我想将具有 jid_str 的用户在线/离线状态保存在 Django DB 中(使用 Django 模型 UserProfile)。我用这个函数来做:

def django_useravailable(jid_str, user_available):
    尝试:
        userhost = jid.JID(jid_str).userhost()
        用户 = UserProfile.objects.get(im_jabber_name=userhost)
        user.im_jabber_online = user_available
        用户.save()
        返回 jid_str, user_available
    除了异常,e:
        打印 e
    引发 jid_str, user_available,e

目前,我通过以下方式调用它:

d = threads.deferToThread(django_useravailable, from_attr, user_available)
d.addCallback(self.success)
d.addErrback(self.failure)

I have a normal Django site running. In addition, there is another twisted process, which listens for Jabber presence notifications and updates the Django DB using Django's ORM.

So far it works as I just call the corresponding Django models (after having set up the settings environment correctly). This, however, blocks the Twisted app, which is not what I want.

As I'm new to twisted I don't know, what the best way would be to access the Django DB (via its ORM) in a non-blocking way using deferreds.

  1. deferredGenerator ?
  2. twisted.enterprise.adbapi ? (circumvent the ORM?)
  3. ???

If the presence message is parsed I want to save in the Django DB that the user with jid_str is online/offline (using the Django model UserProfile). I do it with that function:

def django_useravailable(jid_str, user_available):
    try:
        userhost = jid.JID(jid_str).userhost()
        user = UserProfile.objects.get(im_jabber_name=userhost)
        user.im_jabber_online = user_available
        user.save()
        return jid_str, user_available
    except Exception, e:
        print e
    raise jid_str, user_available,e

Currently, I invoke it with:

d = threads.deferToThread(django_useravailable, from_attr, user_available)
d.addCallback(self.success)
d.addErrback(self.failure)

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

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

发布评论

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

评论(3

风吹短裙飘 2024-08-15 20:35:42

“我有一个正常的 Django 站点正在运行。”

大概在 Apache 下使用 mod_wsgi 或类似的。

如果您使用 Apache 中嵌入的 mod_wsgi,请注意 Apache 是多线程的,并且您的 Python 线程会合并到 Apache 的线程中。对阻塞的分析可能会变得很棘手。

如果您在守护进程模式下使用 mod_wsgi (您应该如此),那么您的 Django 是一个单独的进程。

为什么不继续这种设计模式并使您的“jabber 侦听器”成为一个单独的进程。

如果您希望此进程在任意多个服务器中运行,则可以从 init.rccron 启动它。

因为它是一个单独的过程,所以不会争夺注意力。您的 Django 进程运行速度很快,并且您的 Jabber 侦听器独立运行。

"I have a normal Django site running."

Presumably under Apache using mod_wsgi or similar.

If you're using mod_wsgi embedded in Apache, note that Apache is multi-threaded and your Python threads are mashed into Apache's threading. Analysis of what's blocking could get icky.

If you're using mod_wsgi in daemon mode (which you should be) then your Django is a separate process.

Why not continue this design pattern and make your "jabber listener" a separate process.

If you'd like this process to be run any any of a number of servers, then have it be started from init.rc or cron.

Because it's a separate process it will not compete for attention. Your Django process runs quickly and your Jabber listener runs independently.

吻泪 2024-08-15 20:35:42

我已经成功地使用您描述的当前方法。通过阅读文档,您会发现扭曲的 DB api 在底层使用线程,因为大多数 SQL 库都有阻塞 API。

我有一个扭曲的服务器,用于保存现场电源监视器的数据,它通过时不时地启动一个子线程并调用我的 Django 保存代码来实现。您可以阅读有关我的实时数据的更多信息收集管道(这是一个博客链接)。

您是说您正在启动一个子线程,并且该子线程仍然处于阻塞状态吗?

I have been successful using the method you described as your current method. You'll find by reading the docs that the twisted DB api uses threads under the hood because most SQL libraries have a blocking API.

I have a twisted server that saves data from power monitors in the field, and it does it by starting up a subthread every now and again and calling my Django save code. You can read more about my live data collection pipeline (that's a blog link).

Are you saying that you are starting up a sub thread and that is still blocking?

乞讨 2024-08-15 20:35:42

我有一个正在运行的 Twisted 应用程序,我在其中使用 Django ORM。我不会推迟它。我知道这是错误的,但还没有任何问题。

I have a running Twisted app where I use Django ORM. I'm not deferring it. I know it's wrong, but hadd no problems yet.

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