I have an upload page in django powered site. The end-users upload the documents from that page. The back end processing of that uploaded document can take several minutes. How do i handle multiple end-users' request in the back end? I have thought of using thread for each end-user's request. However, i am finding it difficult in coding that how do i create a new thread as soon as some user uploads a document. A sample example or demo would be highly appreciated. Thanking you in advance!!!
2 Answers
Without more information its hard to say what exactly, but threads sound like a reasonable idea. To create a thread in python, you do the following:
from threading import Thread
class Worker(Thread):
def __init__(self):
Thread.__init__(self) #Runs the thread's constructor
#Method that is run when the new thread starts
def run(self):
#Whatever data processing you have to do can go here
while True:
print "Hello from Worker"
w = Worker()
w.start() #Starts a new thread which executes the object's run function
2 Comments
consumer
Thanks for your advice. Do i have to use a callback handler so that if any user uploads a document, this handler automatically spawns a new thread for that user's request?
JohnS
If you're uploading the files asynchronously (e.g. using Ajax) and you could use a callback to do something client-side in Javascript when the processing of the file is finished (such as redirecting the user to a new page).