Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
fw:celery [2014/08/17 16:15] alfred [Tasks] |
fw:celery [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 412: | Línea 412: | ||
| add.name | add.name | ||
| </code> | </code> | ||
| - | The bind parameter for ''@task'' allows to the task to recibe information of its calling [[http://docs.celeryproject.org/en/latest/userguide/tasks.html#context]]. The data is accessed in the ''self'' variable. \\ | + | The bind parameter for ''@task'' allows to the task to recibe information of its calling using the [[http://docs.celeryproject.org/en/latest/userguide/tasks.html#context|request variable]]. The data is accessed in the ''self'' variable. \\ |
| You can also use the ''.retry()'' method to send the same task again. This method cuts the execution of the task, it's because it throws an exception. If you don't want this behaviour set the ''throw'' parameter for ''retry()'' as ''False''. There also is the ''max_retries'' properties. Or the ''default_retry_delay'' for the task (default set as 30 secs). | You can also use the ''.retry()'' method to send the same task again. This method cuts the execution of the task, it's because it throws an exception. If you don't want this behaviour set the ''throw'' parameter for ''retry()'' as ''False''. There also is the ''max_retries'' properties. Or the ''default_retry_delay'' for the task (default set as 30 secs). | ||
| <code python> | <code python> | ||
| Línea 540: | Línea 540: | ||
| ==== Testing ==== | ==== Testing ==== | ||
| With the configuration ''CELERY_ALWAYS_EAGER'' assigned as True, all the workers are called without any asynchronous behavior. | With the configuration ''CELERY_ALWAYS_EAGER'' assigned as True, all the workers are called without any asynchronous behavior. | ||
| + | ==== Periodic tasks ==== | ||
| + | You can call Celery tasks in concrete moments: [[http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html| periodic tasks guide]]. To do so you need a beat process (only one) to call the workers when it's needed. Next example are two files: | ||
| + | * **beat.py**, which is executed as: ''celery -A beat beat'' | ||
| + | <code python> | ||
| + | from datetime import timedelta | ||
| + | from celery import Celery | ||
| + | from tasks import app | ||
| + | |||
| + | app.conf.update( | ||
| + | CELERYBEAT_SCHEDULE = { | ||
| + | 'add-every-30-seconds': { | ||
| + | 'task': 'tasks.add', | ||
| + | 'schedule': timedelta(seconds=10) | ||
| + | }, | ||
| + | }, | ||
| + | CELERY_TIMEZONE = 'UTC' | ||
| + | ) | ||
| + | </code> | ||
| + | * **tasks.py**, which is executed as: ''celery -A tasks worker'' | ||
| + | <code python> | ||
| + | from celery import Celery | ||
| + | import datetime | ||
| + | |||
| + | app = Celery('tasks', broker='amqp://guest@192.168.0.100//') | ||
| + | |||
| + | @app.task | ||
| + | def add(): | ||
| + | f = open('/home/alfred/a.txt', 'a') | ||
| + | f.write(str(datetime.datetime.now())) | ||
| + | f.close() | ||
| + | </code> | ||
| + | |||
| ==== Little tools ==== | ==== Little tools ==== | ||
| - | === Periodic tasks === | + | |
| - | You can call Celery tasks in concrete moments: [[http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html| periodic tasks guide]]. | + | |
| === HTTP Callbacks === | === HTTP Callbacks === | ||
| Línea 648: | Línea 679: | ||
| ==== Notes ==== | ==== Notes ==== | ||
| * If you want to use print you must set ''CELERY_REDIRECT_STDOUTS'' to ''False''. | * If you want to use print you must set ''CELERY_REDIRECT_STDOUTS'' to ''False''. | ||
| + | |||
| + | === Execute celery task from c# === | ||
| + | <code csharp> | ||
| + | using (var channel = connection.CreateModel()) | ||
| + | { | ||
| + | var guid = Guid.NewGuid().ToString(); | ||
| + | string message = String.Format("{{\"id\": \"{0}\", \"task\": \"my.task\", \"args\": [1, 2]}}", guid); | ||
| + | var body = Encoding.UTF8.GetBytes(message); | ||
| + | |||
| + | IBasicProperties props = channel.CreateBasicProperties(); | ||
| + | props.ContentType = "application/json"; | ||
| + | props.ContentEncoding = "UTF-8"; | ||
| + | //channel.QueueDeclare("celery", true, true, false, null); | ||
| + | channel.BasicPublish("celery", "celery", props, body); | ||
| + | |||
| + | Console.WriteLine(" [x] Sent {0}", message); | ||
| + | } | ||
| + | </code> | ||
| + | When task is: | ||
| + | <code python> | ||
| + | app = Celery('tasks', broker='amqp://guest@192.168.0.100//') | ||
| + | @app.task(name='my.task', ignore_result=True) | ||
| + | def add(a, b): | ||
| + | logger = get_task_logger(__name__) | ||
| + | logger.critical('tralara lalala ' + str(a+b)) | ||
| + | </code> | ||
| + | |||