trying to learn web2py with celery, but an error occured
*- install celery*
source activate test2
pip install celery
*- start redis server from source install (stable version 4.0.9)*
./src/redis-server
*- start web2py (stable version 2.16.1 on python 2.7)*
source activate test2
python ~/python/web2py/web2py.py --nogui --no-banner -a a -i 0.0.0.0 -p 8000
*- create new web2py app named : celery*
*- create modules*
modules/w2p_celery.py
from celery import Celery
mycelery = Celery('tasks', broker='redis://localhost:6379/0',
backend='redis://localhost:6379/0')
*- create models*
*models/thecelerymodel.py*
from w2p_celery import mycelery
celery = mycelery
@celery.task(name='tasks.gen_url')
def gen_url(x):
return A(x, _href=URL('rule_the_world'))
@celery.task(name='tasks.add_user')
def add_user():
try:
db.auth_user.insert(first_name='John')
db.commit()
except:
db.rollback()
*- create tasks.py in the same folder as web2py.py*
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gluon.widget #forgot: why this is apparently the only way to
fix custom imports ?
from gluon.shell import env
from gluon import current
from celery import Celery
def make_celery(app):
celery = Celery('tasks', broker='redis://localhost:6379/0',
backend='redis://localhost:6379/0')
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
_env = env(a=app, import_models=True)
globals().update(_env)
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
celery = make_celery('celery') #be sure that you write this correctly
@celery.task(name='tasks.gen_url')
def gen_url(x):
return A(x, _href=URL(x, 'rule_the_world'))
@celery.task(name='tasks.add_user')
def add_user(): #yes, for the love of your apps, wrap all db operations!
try:
db.auth_user.insert(first_name='miao')
db.commit()
except:
db.rollback()
*- start celery*
cd ~/python/web2py/
source activate test2
python
>>> import celery
>>> celery worker -A tasks
File "<stdin>", line 1
celery worker -A tasks
^
SyntaxError: invalid syntax
any idea?
thanks and best regards,
stifan
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.