I have a celery task function which takes as attribute a datetime object. Inside the celery task however this object is a string. I imagine this is serialization at work.
How do I perform deserialization on the other end? Do I have to handle it on my own or is there a more elegant way to do it that's built in in celery?
# tasks.py
@shared_task
def my_func(start_time, end_time):
print(type(start_time)) # unicode, need datetime
# calling file
my_func.delay(datetime.now()-timedelta(hours=1), datetime.now())
This way, you delegate queue creation to Celery. You can use apply_async with any queue and Celery will handle it, provided your task is aware of the queue used by apply_async . If none is provided then the worker will listen only for the default queue.
If you look at the celery DOCS on tasks you see that to call a task synchronosuly, you use the apply() method as opposed to the apply_async() method. The DOCS also note that: If the CELERY_ALWAYS_EAGER setting is set, it will be replaced by a local apply() call instead.
Celery has the ability to communicate and store with many different backends (Result Stores) and brokers (Message Transports).
You can try using another serializer. You are presumably using the default json serializer, which means datetimes must be represented as strings. The other supported serializers should be able to handle datetimes.
http://docs.celeryproject.org/en/latest/userguide/calling.html#serializers
# calling file
my_func.apply_async(
args=[datetime.now()-timedelta(hours=1), datetime.now()],
serializer='pickle', # or 'yaml' or 'msgpack'
)
We can't use the celery shortcut function delay
if we want to pass execution options, so we use the underlying apply_async
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With