简体   繁体   English

Django-Celery 数据库访问

[英]Django-Celery Database Access

django-celery-beat running so far ... tasks get queued and worker is executing simple tasks :) django-celery-beat 运行到目前为止......任务排队并且工作人员正在执行简单的任务:)

now I would like to have my celery task to do a query towards the django database (to get a list of IP addresses for which I want to check reachability).现在我想让我的 celery 任务对 django 数据库进行查询(以获取我想要检查可达性的 IP 地址列表)。

can i use the django 'helper code' to do the query or do I need to make a 'raw' connection from celery to sqlite/mysql?我可以使用 django 'helper code' 进行查询,还是需要建立从 celery 到 sqlite/mysql 的“原始”连接?

thanks for any help on this感谢您对此的任何帮助

/pat /拍

celery.py芹菜.py

# http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
#from celery import shared_task
from django.conf import settings
#settings.configure()
#from portal import models
#logger = logging.getLogger(__name__)
#from portal.models import location

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sc_custportal.settings')

app = Celery('sc_custportal')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
#app.config_from_object('django.conf:settings')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))
    queryset = location.objects.all()
#     response = os.system("ping -c 1 8.8.8.8")
#     if response == 0:
#         print("up!")

error:错误:

[2017-05-04 14:55:09,962: ERROR/PoolWorker-1] Task sc_custportal.celery.debug_task[ecb79b33-7246-4e4e-9b4e-3d7f9b0102dc] raised unexpected: NameError("global name 'location' is not defined",)
Traceback (most recent call last):
  File "/home/pat/Documents/Development/sc_custportal/env/local/lib/python2.7/site-packages/celery/app/trace.py", line 367, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/pat/Documents/Development/sc_custportal/env/local/lib/python2.7/site-packages/celery/app/trace.py", line 622, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/pat/Documents/Development/sc_custportal/sc_custportal/sc_custportal/celery.py", line 32, in debug_task
    queryset = location.objects.all()
NameError: global name 'location' is not defined

tree:树:

sc_custportal
├── celerybeat.pid
├── celerybeat-schedule
├── db.sqlite3
├── manage.py
├── portal
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── 0002_auto_20170503_1212.py
│   │   ├── 0002_auto_20170503_1212.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   ├── index.html
│   │   ├── inspinia.html
│   │   ├── issue_tracker.html
│   │   ├── login.html
│   │   ├── login.html.sic
│   │   ├── modal_window.html
│   │   ├── notifications.html
│   │   ├── partials
│   │   │   ├── footer.html
│   │   │   ├── head.html
│   │   │   ├── menu.html
│   │   │   └── scripts.html
│   │   ├── profile.html
│   │   ├── table_data_tables.html
│   │   └── toastr_notifications.html
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── rest
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── serializers.py
│   ├── serializers.pyc
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
└── sc_custportal
    ├── celery.py
    ├── celery.pyc
    ├── __init__.py
    ├── __init__.pyc
    ├── settings.py
    ├── settings.pyc
    ├── settings.py.orig
    ├── urls.py
    ├── urls.pyc
    ├── wsgi.py
    └── wsgi.pyc

Yes, you can do it like是的,你可以这样做

from portal.models import MyModel

@app.task(bind=True)
def debug_task(self):
    MyModel.objects.all()

If you're considering accessing objects recently created/updated, please take care that the transaction commited before starting your task.如果您正在考虑访问最近创建/更新的对象,请注意在开始您的任务之前提交的事务。 Typically, you could start your task with a transaction.on_commit(your_task_call) .通常,您可以使用transaction.on_commit(your_task_call)开始您的任务。 This answer gives you more details.这个答案为您提供了更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM