On my django app I have a report (a csv download) that can take some time to run. When a user runs the report they are redirected to a ‘processing’ page where a javascript function checks the server every second to see if the csv has been created (the file name is included in the HttpResponse object).
What I’m looking for is a way of identifying the thread that’s creating the csv. That way I can add an estimated_time_to_completion attribute to the thread, and include this info in the holding page. In fact I could stop checking for the existance of the (unlocked) csv – I could just ask the thread if it’s finished.
My csv building thread looks something like –
class CsvBuilder(threading.Thread):
def __init__(self, file_name, parameters)
self.file_name = file_name
self.parameters = parameters
threading.Thread.__init__(self)
def run():
# ...
file = open(self.file_name, 'wb')
writer = csv.writer(file)
for patient in patients:
writer.writerow('some data')
self.time_remaining = # a timedelta object
file.close()
And then my django requests will look something like –
def create_csv(request):
'''
Standard django view to create a csv
'''
# get filename and parameters from request
thread = CsvBuilder (file_name, parameters)
return render_to_response('processing.html', {"thread_id": thread.thread_id})
def check_progress(request):
'''
An ajax call to check the progress on a report
'''
thread_id = requst.GET['thread_id']
# find the thread
return HttpResponse(thread.time_remaining)
Is this possible? Or should I be going about this a different way?
I’d suggest you have your writer function update a memcached key/value for time_remaining calculations.
If it were me, I’d have probably used Celery for the long running job, starting a thread from django seems like it could have pitfalls, but nothing specific is springing to mind.