views:

100

answers:

2
+1  A: 

You don't want threading. You want a work queue like Gearman that you can send jobs to asynchronously.

It's worth noting that this is a cross-platform, cross-language solution. There are bindings for many languages (including Python and PHP) provided officially, and many more unofficially with a bit of work with Google.

The original intent is effectively load balancing, but it works just as well with only one machine. Basically, you can create one or more Workers that listen for Jobs. You can control the number of Workers and the types of Jobs they can listen for.

If you insert five Jobs into the queue at the same time, and there happen to be five Workers waiting, each Worker will be handed one of the Jobs. If there are more Jobs than Workers, the Jobs get handled sequentially. Your Client (the thing that submits Jobs) can either wait for all of the Jobs it's created to complete, or it can simply place them in the queue and continue on.

Charles
Will check it, thanks.
jahmax
But isn't gearman build for distributing tasks between "multiple computers"? If I only use one computer what benefits will it give to my app?
jahmax
I've updated my answer to clarify.
Charles
+1  A: 

For Python 2.6+, consider the multiprocessing module:

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows

For Python 2.5, the same functionality is available via pyprocessing.

In addition to the example at the links above, here are some additional links to get you started:

ars
That looks like it will do the job, will read more about it later, thanks.
jahmax