Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 304
mm_food Avatar asked Jul 30 '10 15:07

mm_food


2 Answers

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.

like image 86
Daniel Roseman Avatar answered Sep 25 '22 11:09

Daniel Roseman


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.

like image 40
Daniel Kluev Avatar answered Sep 25 '22 11:09

Daniel Kluev