Overriding the Celery Task Class

Am trying to implement a task where the global variables are shared between two different Celery tasks. For that, I have inherited task class and used property. As per celery documentation the base class will initialize when a new task is invoked. Do we have an approach where can reuse the object in between tasks? Can we override the run() method from Task? If we override the run method. How can we register the task ? with celery using Celery 5. X ? Tried Serializing object.. Any alternate approach would be appreciated.

class handler(Task):

def __init__(self):
    self.base_obj = ""

def global_handler(self):
    return self.global_thread_handler

def base_handler(self):
    return self.base_obj

def test123():
    test123.base_handler = cls1()

def test456():

Registering a task in Celery can be simply done using something like this: # my_app/tasks.py import celery

from my_app.celery import app

class MyTask(celery.Task): def run(self): [...]

MyTask = app.register_task(MyTask())

I think there's no way you can reuse the objects within tasks. Can someone correct me on this?

Back to Top