Celery | Flask error: expected a bytes-like object, AsyncResult found Celery | Flask error: expected a bytes-like object, AsyncResult found flask flask

Celery | Flask error: expected a bytes-like object, AsyncResult found


EXPLANATION :

When you call your task :

task_id = update_trading_pair.delay(exchange, currency_a, currency_b)

your variable task_id is an instance of AsyncResult, it is not a string.

So, your variable TASK_STATES[market.__id__()] is also an instance of AsyncResult whereas it should be a string.

And then your are trying to instantiate an AsyncResult object with it

result = update_trading_pair.AsyncResult(TASK_STATES[market.__id__()])

So you are instantiating an AsyncResult object with another AsyncResult object whereas it should be instantiated with a string.

Maybe your confusion comes from your print(task_id) which shows you a string, but when you do this, under the hood the __str__ method of the AsyncResult object is called and if you look at it in the source code here,

def __str__(self):    """`str(self) -> self.id`."""    return str(self.id)

it justs print the id attribute of your task_id object.

SOLUTION :

You can fix it with either by doing TASK_STATES[id] = task_id.id or by doing

result = update_trading_pair.AsyncResult(str(TASK_STATES[market.__id__()]))