Ho configurato il mio progetto, facendo riferimento a questa risposta: How to use Flask-SQLAlchemy in a Celery taskcompiti sedano Flask non lavorano
mio extension.py
di file:
import flask
from flask.ext.sqlalchemy import SQLAlchemy
from config import BaseConfig
from celery import Celery
from flask_mail import Mail
from celery.schedules import crontab
class FlaskCelery(Celery):
def __init__(self, *args, **kwargs):
super(FlaskCelery, self).__init__(*args, **kwargs)
self.patch_task()
if 'app' in kwargs:
self.init_app(kwargs['app'])
def patch_task(self):
TaskBase = self.Task
_celery = self
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
if flask.has_app_context():
return TaskBase.__call__(self, *args, **kwargs)
else:
with _celery.app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
self.Task = ContextTask
def init_app(self, app):
self.app = app
self.config_from_object(app.config)
mail = Mail()
db = SQLAlchemy()
settings = BaseConfig()
celery = FlaskCelery()
Poi nel mio app_settings.py
, ho creato questa app:
app = Flask('app', instance_relative_config=True)
E sedano configurato:
celery.init_app(app)
ho eseguito il progetto di pallone con python manage.py run
:
app.run(
debug=settings.get('DEBUG', False),
host=settings.get('HOST', '127.0.0.1'),
port=settings.get('PORT', 5000)
)
e corse sedano:
celery -A manage.celery worker --beat -l debug
registro Sedano sembra buono:
[tasks]
. app.api.tasks.spin_file
. app.main.tasks.send_async_email
. celery.backend_cleanup
. celery.chain
...
Poi nel views.py
, io chiamo questo compito :
send_async_email.delay(*args, **kwargs)
Ma tutti i compiti vengono ignorati da Celery. Non succede nulla, nessun errore, nessun avvertimento. Niente. Che cosa sto facendo di sbagliato?
EDIT: Quando inizio sedano con questo comando: celery -A manage.celery worker --beat -l debug
ottengo il seguente avvertimento:
[2015-09-21 10:04:32,220: WARNING/MainProcess] /home/.virtualenvs/myproject/local/lib/python2.7/site-packages/celery/app/control.py:36: DuplicateNodenameWarning: Received multiple replies from node name: 'name'.
Please make sure you give each node a unique nodename using the `-n` option.
pluralize(len(dupes), 'name'), ', '.join(sorted(dupes)),
Tale errore fa sembrare come si sta già eseguendo un lavoratore. Uccidere tutti i lavoratori del sedano esistenti prima di riavviare il lavoratore. Il lavoratore non vedrà le modifiche nel codice finché non lo riavvii. – davidism