Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django芹菜数据库访问_Python_Mysql_Django_Sqlite_Django Celery - Fatal编程技术网

Python Django芹菜数据库访问

Python Django芹菜数据库访问,python,mysql,django,sqlite,django-celery,Python,Mysql,Django,Sqlite,Django Celery,django芹菜击败了目前为止的跑步。。。任务进入队列,工作人员正在执行简单任务:) 现在我想让我的芹菜任务对django数据库进行查询(以获取我想要检查其可达性的IP地址列表) 我可以使用django“helper code”进行查询,还是需要从芹菜到sqlite/mysql建立“原始”连接 谢谢你在这方面的帮助 /拍 芹菜 # http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html from __

django芹菜击败了目前为止的跑步。。。任务进入队列,工作人员正在执行简单任务:)

现在我想让我的芹菜任务对django数据库进行查询(以获取我想要检查其可达性的IP地址列表)

我可以使用django“helper code”进行查询,还是需要从芹菜到sqlite/mysql建立“原始”连接

谢谢你在这方面的帮助

/拍

芹菜

# 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!")
错误:

[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
树:


是的,你可以这样做

from portal.models import MyModel

@app.task(bind=True)
def debug_task(self):
    MyModel.objects.all()
如果您正在考虑访问最近创建/更新的对象,请注意在开始任务之前提交事务。通常,您可以使用
事务启动任务。在提交时(您的任务调用)
。 给你更多的细节

from portal.models import MyModel

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