views:

93

answers:

2
+5  Q: 

When to thread?

I have never written any code that uses threads.

I have a web application that accepts a POST request, and creates an image based on the data in the body of the request.

Would I want to spin off a thread for the image creation, as to prevent the server from hanging until the image is created? Is this an appropriate use, or merely a solution looking for a problem ?

Please correct any misunderstandings I may have.

+2  A: 

Usual approach for handling HTTP requests synchronously is to spawn (or re-use one in the pool) new thread for each request as soon as it comes.

However, python threads are not very good for HTTP, due to GIL and some i/o and other calls blocking whole app, including other threads.

You should look into multiprocessing module for this usage. Spawn some worker processes, and then pass requests to them to process.

Daniel Kluev
+3  A: 

Rather than thinking about handling this via threads or even processes, consider using a distributed task manager such as Celery to manage this sort of thing.

Daniel Roseman
What does this buy me?
mm_food
@mm_food, as I understand, it lets you use several hosts for computing at once, plus it handles parallelization using multiprocessing by itself.
Daniel Kluev
I checked out the site and I saw Celery handles cron-like tasks, which is something I would like to use for the same project. Thanks for the suggestion.
mm_food