Django Celery 和多个数据库(Celery、Django 和 RabbitMQ)

发布于 2024-10-27 11:31:37 字数 154 浏览 2 评论 0原文

是否可以设置与 Django Celery 一起使用的不同数据库?

我有一个配置了多个数据库的项目,并且不希望 Django Celery 使用默认数据库。

如果我仍然可以使用 django celery 管理页面并读取存储在这个不同数据库中的结果,我会很好:)

Is it possible to set a different database to be used with Django Celery?

I have a project with multiple databases in configuration and don't want Django Celery to use the default one.

I will be nice if I can still use django celery admin pages and read results stored in this different database :)

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

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

发布评论

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

评论(2

橘寄 2024-11-03 11:31:37

是的,你可以。

第一:您可以设置两个数据库并为 celery 任务显式指定第二个数据库(例如 obj.save(using='second'))

或创建第二个 settings.py< /code> 将用于芹菜:

./manage.py celeryd --settings_second

yes you can.

first: you can set-up a two databases and specify second one explicitly in for the celery tasks (e.g. obj.save(using='second'))

or create second settings.py which will be used for the celery:

./manage.py celeryd --settings_second
等待我真够勒 2024-11-03 11:31:37

应该可以使用 Django 数据库路由器为 django-celery 模型设置一个单独的数据库:

https://docs.djangoproject.com/en/1.4/topics/db/multi-db/#automatic-database-routing

我没有专门对此进行测试与 django-celery 一起使用,但如果由于某种原因它不起作用,那么这是 django-celery (或 Django 本身)中的一个错误,应该修复。

您的路由器看起来像这样:

class CeleryRouter(object):
    "Route Celery models to separate DB."
    APPS = (
        'django',  # Models from kombu.transport.django, if you're using Django as a message transport.
        'djcelery',
    )
    DB_ALIAS = 'celery'

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.APPS:
            return self.DB_ALIAS
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.APPS:
            return self.DB_ALIAS
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if (obj1._meta.app_label in self.APPS and
            obj2._meta.app_label in self.APPS):
            return True
        return None

    def allow_syncdb(self, db, model):
        if db == self.DB_ALIAS:
            # Only put models from APPS into Celery table (and south for
            # migrations).
            return model._meta.app_label in self.APPS + ('south',)
        elif model._meta.app_label in self.APPS:
            # Don't put Celery models anywhere else.
            return False
        return None

然后将其添加到您的设置中:

DATABASE_ROUTERS = ['path.to.CeleryRouter']

It should be possible to set up a separate database for the django-celery models using Django database routers:

https://docs.djangoproject.com/en/1.4/topics/db/multi-db/#automatic-database-routing

I haven't tested this specifically with django-celery, but if it doesn't work for some reason, then it's a bug in django-celery (or Django itself) that should be fixed.

Your router would look something like this:

class CeleryRouter(object):
    "Route Celery models to separate DB."
    APPS = (
        'django',  # Models from kombu.transport.django, if you're using Django as a message transport.
        'djcelery',
    )
    DB_ALIAS = 'celery'

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.APPS:
            return self.DB_ALIAS
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.APPS:
            return self.DB_ALIAS
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if (obj1._meta.app_label in self.APPS and
            obj2._meta.app_label in self.APPS):
            return True
        return None

    def allow_syncdb(self, db, model):
        if db == self.DB_ALIAS:
            # Only put models from APPS into Celery table (and south for
            # migrations).
            return model._meta.app_label in self.APPS + ('south',)
        elif model._meta.app_label in self.APPS:
            # Don't put Celery models anywhere else.
            return False
        return None

Then add this to your settings:

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