threading

Ben Finney ben+python at benfinney.id.au
Sun Apr 6 23:05:03 EDT 2014


Onder Hazaroglu <oxhazaroglu at ualr.edu> writes:

> I've been using threading library to run some experiments parallel.

Threading is very difficult to get right, much more so than the usual
separation into distinct processes. How did you decide against the
normal means of parallel execution and instead choose threading?

> There is no message passing between my threads but still it messes up
> somehow. The results are different than running it separated.

Yes, this is a common result of threading; problems are much harder to
reproduce and much more entangled with apparently unrelated factors.
This is one of the main reasons to avoid threading if at all feasible.

> Is there a way to make sure that there is no memory sharing between
> threads?

The *whole point* of threading (AFAIK) is to share memory and other
process-distinct resources. If you're looking to avoid that, don't use
threading! Instead, use separate processes for parallel execution.

Parallel processing is achieved much more reliably and deterministically
with separate processes. Python even makes this much easier than most
languages, with the ‘multiprocessing’ module in the standard library
<URL:https://docs.python.org/3/library/multiprocessing.html>.

I would recommend you make easier-to-understand and easier-to-debug code
with that module, and only consider threading if you determine it's
needed.

-- 
 \                “Stop — Drive sideways.” —detour sign, Kyushu, Japan |
  `\                                                                   |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list