@sortfact333

Как привильно настроить settings.py для celery?

Помогите настроить celery на Windows 10
Я хочу запускать периодичиски задачи в django
Но почти все туториалы написаны для linux
Я уже 3 неделю не могу найти решение

Я пытался запустить celery используя это:

Установил докер rabbitmq-server-3.8.12

Добавил в settings.py
INSTALLED_APPS = [
    'django_celery_results',  
    'django_celery_beat',  
}

CELERY_TIMEZONE = "Australia/Tasmania"
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60

CELERY_RESULT_BACKEND = 'django-db'
CELERY_RESULT_BACKEND = 'django-cache'
CELERY_CACHE_BACKEND = 'default'
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'my_cache_table',
    }
}


Создал файл celery.py
import os
from celery import Celery

name='shop'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', name+'.settings')
app = Celery(name)
app.config_from_object('django.conf:settings', namespace='CELERY')

app.conf.beat_schedule = {
    'every-15-seconds':{
        'task':'offers.tasks.send_email',
        'schedule':15,
        'args':('dsssew.com',)
    }
}
app.conf.timezone = 'UTC'
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')

Создал файл tasks.py
from __future__ import absolute_import, unicode_literals
from celery import shared_task

@shared_task
def send_email(email):
    print(email)


@shared_task
def add(x, y):
    return x + y


@shared_task
def mul(x, y):
    return x * y


@shared_task
def xsum(numbers):
    return sum(numbers)

Запустил все по очереди

python manage.py runserver
celery -A shop worker -l INFO
celery -A shop beat -l INFO

beat вывел мне

-------------- celery@DESKTOP-IG1UFF4 v5.0.5 (singularity)
--- ***** -----
-- ******* ---- Windows-10-10.0.18362-SP0 2021-02-24 17:16:23
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         shop:0x1f78f2877c8
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . offers.tasks.add
  . offers.tasks.mul
  . offers.tasks.send_email
  . offers.tasks.xsum
  . shop.celery.debug_task

[2021-02-24 17:16:24,625: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2021-02-24 17:16:24,760: INFO/MainProcess] mingle: searching for neighbors
[2021-02-24 17:16:26,211: INFO/MainProcess] mingle: all alone
[2021-02-24 17:16:26,377: WARNING/MainProcess] c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\fixups\django.py:204: UserWarning: Using settings.DEBUG leads to a memory
            leak, never use this setting in production environments!
  leak, never use this setting in production environments!''')

[2021-02-24 17:16:26,377: INFO/MainProcess] celery@DESKTOP-IG1UFF4 ready.
[2021-02-24 17:16:27,589: INFO/SpawnPoolWorker-2] child process 21568 calling self.run()
[2021-02-24 17:16:27,662: INFO/SpawnPoolWorker-1] child process 15700 calling self.run()
[2021-02-24 17:16:27,734: INFO/SpawnPoolWorker-3] child process 17296 calling self.run()
[2021-02-24 17:16:27,901: INFO/SpawnPoolWorker-4] child process 14420 calling self.run()
[2021-02-24 17:16:28,004: INFO/SpawnPoolWorker-5] child process 2672 calling self.run()
[2021-02-24 17:16:28,015: INFO/SpawnPoolWorker-6] child process 24196 calling self.run()
[2021-02-24 17:16:28,025: INFO/SpawnPoolWorker-7] child process 16796 calling self.run()
[2021-02-24 17:16:28,324: INFO/SpawnPoolWorker-8] child process 22716 calling self.run()

И каждые 15 секунд это
[2021-02-19 16:18:02,275: WARNING/SpawnPoolWorker-1] c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\app\trace.py:617: RuntimeWarning: Exception raised outside body: TypeError('ord() expected string of length 1, but int found'):
Traceback (most recent call last):
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\app\trace.py", line 400, in trace_task
    request=task_request,
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\backends\base.py", line 435, in store_result
    request=request, **kwargs)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\backends\base.py", line 851, in _store_result
    current_meta = self._get_task_meta_for(task_id)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\backends\base.py", line 869, in _get_task_meta_for
    meta = self.get(self.get_key_for_task(task_id))
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django_celery_results\backends\cache.py", line 19, in get
    return self.cache_backend.get(key)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django\core\cache\backends\db.py", line 51, in get
    return self.get_many([key], version).get(key, default)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django\core\cache\backends\db.py", line 59, in get_many
    self.validate_key(key)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django\core\cache\backends\base.py", line 249, in validate_key
    for warning in memcache_key_warnings(key):
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django\core\cache\backends\base.py", line 287, in memcache_key_warnings
    if ord(char) < 33 or ord(char) == 127:
TypeError: ord() expected string of length 1, but int found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\app\trace.py", line 536, in trace_task
    return task.__trace__(uuid, args, kwargs, request)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\app\trace.py", line 524, in trace_task
    I, _, _, _ = on_error(task_request, exc, uuid)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\app\trace.py", line 359, in on_error
    task, request, eager=eager, call_errbacks=call_errbacks,
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\app\trace.py", line 170, in handle_error_state
    call_errbacks=call_errbacks)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\app\trace.py", line 215, in handle_failure
    call_errbacks=call_errbacks,
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\backends\base.py", line 165, in mark_as_failure
    traceback=traceback, request=request)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\backends\base.py", line 435, in store_result
    request=request, **kwargs)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\backends\base.py", line 851, in _store_result
    current_meta = self._get_task_meta_for(task_id)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\celery\backends\base.py", line 869, in _get_task_meta_for
    meta = self.get(self.get_key_for_task(task_id))
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django_celery_results\backends\cache.py", line 19, in get
    return self.cache_backend.get(key)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django\core\cache\backends\db.py", line 51, in get
    return self.get_many([key], version).get(key, default)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django\core\cache\backends\db.py", line 59, in get_many
    self.validate_key(key)
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django\core\cache\backends\base.py", line 249, in validate_key
    for warning in memcache_key_warnings(key):
  File "c:\users\admin\appdata\local\programs\python\python37\lib\site-packages\django\core\cache\backends\base.py", line 287, in memcache_key_warnings
    if ord(char) < 33 or ord(char) == 127:
TypeError: ord() expected string of length 1, but int found

  exc, exc_info.traceback)))

Я не понимаю в чем ошибка и как это исправить?

Но подозреваю что ошибка где-то в settings.py
  • Вопрос задан
  • 381 просмотр
Пригласить эксперта
Ответы на вопрос 1
sortfact333 у вас ошибка в этом пакете django_celery_results пробуйте без него запустить.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы