Search code examples
pythondjangoceleryglobal-variablesdjango-celery

Global variable with Django and Celery


I have a code like this,

wl_data = {}

def set_wl_data():
    global wl_data
    wl_data = get_watchlist_data()


def get_wl_data(scripcodes):
    # Filtering Data
    result = {scripcode:detail for scripcode, detail in wl_data.iteritems() if int(scripcode) in scripcodes or scripcode in scripcodes}
    return result

I am running this as a django project, I am calling the setter method from celery, to update the global variable wl_data.
tastypie api will call the getter method get_wl_data to fetch global variable wl_data.

The problem is celery is updating wl_data properly. But when we hit the tastypie api url in browser, the getter method serves the old data.

There are so many related questions in stack overflow, but the difference here is setter method is called by celery task. Please help me to solve this issue.


Solution

  • If you're doing anything with global variables in a Django project, you're doing it wrong. In this case, Celery and Django are running in completely separate processes, so cannot share data. You need to get Celery to store that data somewhere - in the db, or a file - so that Django can pick it up and serve it.