I created a long running Celery worker that handles web scraping and populates a dictionary object. I created multiple jobs that run asynchronously and all of which populate the same dict object. I need a way to dump the result in my worker to a JSON file. It can be done periodically or once the jobs are done, but it should not block my API response.
@celery.task(name="tasks.workertask")
def worker(d, x):
"""
Do some long task on x and populate the dictionary d
"""
def collect(x_list)
d = dict()
for x in x_list:
worker.delay(d,x)
return d.copy()
@application.route('/api')
def api():
#Store x_list
x_list = [...]
d = collect(x_list)
with open('result.json','w') as fp:
json.dump(d,fp)
return "Dumping into JSON"
The dump into result.json is empty because the task is running in the background. I don't want this task to be blocking my API response either as the task is very very long (more than 12 hours)