Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I deserialize a datetime string in celery?

Tags:

python

celery

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())
like image 443
Alex Avatar asked Feb 15 '18 16:02

Alex


People also ask

What is Apply_async in Celery?

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.

How do you call Celery synchronously?

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.

What is broker and backend in Celery?

Celery has the ability to communicate and store with many different backends (Result Stores) and brokers (Message Transports).


1 Answers

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.

like image 172
Håken Lid Avatar answered Oct 09 '22 02:10

Håken Lid