Skip to content

Instantly share code, notes, and snippets.

@shane0
Last active October 20, 2017 18:15
Show Gist options
  • Save shane0/fe304d696ab2bf4d4e3a6ef0b9609f89 to your computer and use it in GitHub Desktop.
Save shane0/fe304d696ab2bf4d4e3a6ef0b9609f89 to your computer and use it in GitHub Desktop.
celery beat
celery -A beat beat -l debug
from celery import Celery
from web_tasks import Web
celery = Celery()
celery.config_from_object()
@celery.task
def web_task(site):
print('running a web task')
Web.open_url(site)
from celery.schedules import crontab
# using rabbitmq
broker_url = 'amqp://user:user@localhost:5672//'
result_backend = 'amqp://user:user@localhost:5672//'
beat_schedule = {
'every-minute': {
'task': 'worker.web_task',
'schedule': crontab(minute='*/1'),
'args': ['http://google.com'],
},
}
flower -A worker --port=5555
# -*- coding: utf-8 -*-
import webbrowser
class Web:
def __init__(self, url):
self.url = url
def open_url(url):
webbrowser.open(url)
celery -A worker worker -l info
from celery import Celery
from web_tasks import Web
celery = Celery()
celery.config_from_object('celeryconfig')
@celery.task
def web_task(site):
print('running a web task')
Web.open_url(site)
@shane0
Copy link
Author

shane0 commented Oct 20, 2017

Attempting to schedule background tasks, I had this working at one point it would launch google.com every minute, but it was very finicky for me sometimes it would work the next time it would fail.

I'm on windows so there are some batch files here to launch flower, worker, and beat. After I get this working I'd like to move tasks out of the worker and beat and have it in tasks.py if that is possible, tried importing tasks.py from config it failed.

my worker gets this error

KeyError: 'tasks.web_task'

Please see
http://docs.celeryq.org/en/latest/internals/protocol.html

think its an import issue, reading the docs...

flower says

Exception | NotRegistered('tasks.web_task')

noticed when my worker runs it registers

. worker.web_tasks

tweaked config now it's...

Exception | ValueError('not enough values to unpack (expected 3, got 0)',)

tweaked more with a new task that isn't passing args

Traceback (most recent call last):
  File "c:\users\shane\envs\celery\lib\site-packages\billiard\pool.py", line 358, in workloop
    result = (True, prepare_result(fun(*args, **kwargs)))
  File "c:\users\shane\envs\celery\lib\site-packages\celery\app\trace.py", line 525, in _fast_trace_task
    tasks, accept, hostname = _loc
ValueError: not enough values to unpack (expected 3, got 0)

might be a fix here

pip uninstall celery
pip install celery==3.1.24

@caseymacphee
Copy link

caseymacphee commented Oct 20, 2017

Hey Shane, nothing standing out as a major issue to me in your setup so just going to recommend some things to try. As for tasks in a file other than your celery config you can try http://docs.celeryproject.org/en/latest/_modules/celery/app/base.html#Celery.autodiscover_tasks. I'm using django so I point to my installed_apps in settings where a task sub package is referenced as a django app but should work the same. Looks like this for me
Using a string here means you won't have to pickle when using windows.

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: myproject.settings.INSTALLED_APPS)

That allows me to define a background task like this - notice the way the task is referenced as a package import because it's been installed (you could do this with setuptools) not relatively imported. Here myproject.tasks is a directory with a tasks.py and other resources and defined in the settings.INSTALLED_APPS (which we autodiscovered).

    'clean_celery_results': {
        'task': 'myproject.tasks.tasks.clean_celery_results',
        'args': [dt.datetime.now() - dt.timedelta(days=45)],
        'schedule': crontab(minute=3, hour=9),
    },

Hopefully at least this gives you something to reference against as a sanity check.

@caseymacphee
Copy link

celery ~= 3.1.18

@caseymacphee
Copy link

For a different project I've had to pin to github because the latest version on pypi are buggy for the features I need to use (solar schedules) so I wouldn't rule out issues with the version you were on.

@shane0
Copy link
Author

shane0 commented Oct 20, 2017

thanks for the tips I'll try that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment