Problem in threading
Duncan Booth
duncan.booth at invalid.invalid
Wed Dec 29 06:25:36 EST 2004
Gurpreet Sachdeva wrote:
> Also the difference of time is not much...
> How do we best optimize our task by using threads... please help...
>
For most tasks splitting the processing into separate threads will result
in an increase in the total time to complete the task. The only times when
it may result in a decrease in the running time are when the time the task
takes does not entirely depend on the time taken by the CPU, or when
multiple CPU's are involved.
Unfortunately the latter case isn't handled well by Python, so don't expect
multiple CPU's to help speed up a multi-threaded Python program by very
much. That leaves the former case: if your task has to stop and wait for
something else to happen (e.g. data to be read from a network, or to be
read from a disc file), then splitting it into multiple threads may allow
the waits to be overlapped with useful processing which could result in an
overall decrease in processing time.
A good use of threads is to improve responsiveness of a system. For example
if you ensure that GUI processing happens on a separate thread from some
CPU intensive computation then you can ensure that the GUI remains
responsive while the computation is running. It won't make the computation
complete any faster (in fact it will probably be slower), but the user will
remain happier. Similarly network applications are usually multi-threaded
so that all requests get a fair chance to complete instead of having to
wait for the slowest to complete before others can run.
If you do have multiple processors available and want to speed up a Python
program then you probably have to look at multiple processes rather than
multiple threads. Alternatively you could move parts of the processing out
of the Python environment by rewriting inner loops in C and releasing the
interpreter lock, but that isn't usually the best option.
More information about the Python-list
mailing list