如何将Django post_save信号作为后台进程运行? [英] How to run a Django post_save signal as a background process?

查看:138
本文介绍了如何将Django post_save信号作为后台进程运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型上附加了 post_save 创建外部API时,会通过来自外部API的json响应加载大量信息.不幸的是,这花了足够长的时间,使得heroku在有机会完成之前就超时了.

I have a post_save attached to a model which loads a substantial amount of information via json response from an external API when it is created. Unfortunately this takes long enough that heroku times out before it ever has a chance of finishing.

进行一些研究后,看来最好的方法是作为后台过程.

After doing some research is seems the best way to do this is as a background process.

异步运行post_save信号的最佳方法是什么?

在阅读完芹菜后,似乎对于一个过程来说太重了.还有其他受信任的方法吗?

After reading about celery it seems too heavy-weight for one process. Are there other methods that are trusted?

from django.db.models.signals import post_save
import urllib2
import json

class Foobar(models.Model):
    # ... fields ... #

def foobar_post_save(sender, instance, created, *args, **kwargs):
    """Load info from external API

    data
    ----
    sender - The model class. (Foobar)
    instance - The actual instance being saved.
    created - Boolean; True if a new record was created.
    *args, **kwargs - Capture the unneeded `raw` and `using`(1.3) arguments.
    """

    if created:
        url_to_open = <api_url>
        resp = urllib2.urlopen(url_to_open)
        data = json.loads(resp.read())
        # ... load data ... #

post_save.connect(foobar_post_save, sender=Foobar)

推荐答案

我最终找到了 rq 的答案其中有一个很好的 Heroku文档

I ended up finding the answer with rq which has a nice Heroku documentation

并实现为:

from django.db.models.signals import post_save
import urllib2
import json
from rq import Queue
from worker import conn

q = Queue(connection=conn)

class Foobar(models.Model):
    # ... fields ... #

    def load_data(self):
        url_to_open = <api_url>
        resp = urllib2.urlopen(url_to_open)
        data = json.loads(resp.read())
        # ... load data ... #

def foobar_post_save(sender, instance, created, *args, **kwargs):
    """Load info from external API

    data
    ----
    sender - The model class. (Foobar)
    instance - The actual instance being saved.
    created - Boolean; True if a new record was created.
    *args, **kwargs - Capture the unneeded `raw` and `using`(1.3) arguments.
    """

    if created:
        q.enqueue(instance.load_data, timeout=600)

post_save.connect(foobar_post_save, sender=Foobar)

这篇关于如何将Django post_save信号作为后台进程运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆