我的程序結構與芹菜django demo完全一樣,工作正常。問題是,在我的demoapp/tasks.py模擬I輸入觸發當app/tasks.py導入模型時,芹菜擊敗週期性任務失敗
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
奇怪,至少可以說一個模型(from .models import Service
)。時間表的整個要點是通過向api發送一個請求來更新數據庫。取出導入聲明並註釋掉適當的代碼後,任務按預期運行。這裏是模型的代碼,但無論模型有多少個字段都會出現錯誤,這使得我相信錯誤不是由於模型編程的方式。我在跟蹤火車。
from django.db import models
# Create your models here.
class Service(models.Model):
id = models.TextField(primary_key=True)
scheduled = models.DateTimeField()
expected = models.DateTimeField(null=True, blank=True) # null means cancelled
service = models.TextField()
platform = models.TextField()
origin = models.TextField()
dest = models.TextField()
編輯:這裏是我的demoapp/tasks.py文件:
# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task
import requests
from .models import Service
from django.db import transaction
import datetime
import pytz
def clean(entry):
def remove_location_lists(i):
"""takes care of services with multiple origins or destinations, taking only the first of each."""
if type(i["origin"]["location"]) is list:
i["origin"] = i["origin"]["location"][0]["locationName"]
else:
i["origin"] = i["origin"]["location"]["locationName"]
if type(i["dest"]["location"]) is list:
i["dest"] = i["dest"]["location"][0]["locationName"]
else:
i["dest"] = i["dest"]["location"]["locationName"]
return i
def parse_time(i):
"""converts the raw time string to a datetime object, and includes some logic to manage crossing midnight."""
if i["expected"] == "Cancelled":
i["service"] = "cancelled"
i["expected"] = None
elif i["expected"] == "Delayed": # blank
i["service"] = "delayed"
i["expected"] = None
else:
i["service"] = "ontime"
i["expected"] = pytz.utc.localize(datetime.datetime.strptime(i["scheduled"], '%H:%M'))
i["scheduled"] = pytz.utc.localize(datetime.datetime.strptime(i["scheduled"], '%H:%M'))
i["scheduled"] = i["scheduled"].replace(day=datetime.date.today().day, month=datetime.date.today().month,
year=datetime.date.today().year)
try:
i["expected"] = i["expected"].replace(day=datetime.date.today().day, month=datetime.date.today().month,
year=datetime.date.today().year)
# include day detection logic!
except Exception as inst:
pass
return i
entry = remove_location_lists(entry)
entry = parse_time(entry)
return entry
@shared_task
def get_trips():
print("run task")
url = "http://www.southernrailway.com/ajax/departures/json/"
get = {'from': 'GTW', 'to': '', 'id': ''}
r = requests.post(url, get)
json = r.json()
unsaved = []
for i in json["arrayServices"]:
entry = {'id': i["serviceID"],
'scheduled': i["std"],
'expected': i["etd"],
'service': i["service"],
'platform': i["platform"],
'origin': i["origin"],
'dest': i["destination"]}
entry = clean(entry)
entry = Service(id=entry['id'], scheduled=entry['scheduled'], expected=entry["expected"],
service=entry['service'], platform=entry['platform'], origin=entry['origin'],
dest=entry['dest'])
unsaved.append(entry)
with transaction.atomic():
for i in unsaved:
i.save()
而且,良好的措施,這裏是我celery.py的內容:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from shitternrailways.tasks import get_trips
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'arlyon.settings')
app = Celery('arlyon')
# 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.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(1, get_trips.s(), name="get trips")
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
和終於完全回溯:
/Users/v/Documents/programming/venvs/arlyon/bin/python /Users/v/Documents/programming/arlyon/manage.py runserver 8000
Traceback (most recent call last):
File "/Users/v/Documents/programming/arlyon/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/v/Documents/programming/venvs/arlyon/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/v/Documents/programming/venvs/arlyon/lib/python3.5/site-packages/django/core/management/__init__.py", line 302, in execute
settings.INSTALLED_APPS
File "/Users/v/Documents/programming/venvs/arlyon/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/Users/v/Documents/programming/venvs/arlyon/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/Users/v/Documents/programming/venvs/arlyon/lib/python3.5/site-packages/django/conf/__init__.py", line 99, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/Users/v/Documents/programming/venvs/arlyon/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/v/Documents/programming/arlyon/arlyon/__init__.py", line 5, in <module>
from .celery import app as celery_app
File "/Users/v/Documents/programming/arlyon/arlyon/celery.py", line 4, in <module>
from shitternrailways.tasks import get_trips
File "/Users/v/Documents/programming/arlyon/shitternrailways/tasks.py", line 5, in <module>
from .models import Service
File "/Users/v/Documents/programming/arlyon/shitternrailways/models.py", line 5, in <module>
class Service(models.Model):
File "/Users/v/Documents/programming/venvs/arlyon/lib/python3.5/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/v/Documents/programming/venvs/arlyon/lib/python3.5/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/Users/v/Documents/programming/venvs/arlyon/lib/python3.5/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Process finished with exit code 1
A在尋找幫助的過程中,可能歸因於年輕的芹菜4。所以我轉向了你們。感謝您提供的任何提示。
任務已加載,但無法使用您提供的方法訪問任務。雖然我可以確認它存在(特別是在app.tasks [「demoapp.tasks.get_trips」])我不能讓它註冊任務。我仍然可以註冊celery.py文件中定義的任務(例如調試任務),因此它必須是任務的名稱。它應該採取簽名文件。是app.tasks [「demoapp.tasks.get_trips」]簽名文件?調用app.tasks [「demoapp.tasks.get_trips」]。s()不會執行任何操作。 – arlyon
即使使用-l調試,在芹菜窗口中給出的信息很少,這很煩人。 – arlyon
芹菜有任務名稱的本地註冊表。你訪問它的方式看起來不正確。我認爲它不應該是app.tasks [「demoapp.tasks.get_trips」]。您可以在默認員工啓動時看到的正確名稱。此處還有一些信息http://docs.celeryproject.org/en/latest/userguide/application.html –