[Tutor] threading mind set

Russel Winder russel at winder.org.uk
Sun May 13 13:43:26 CEST 2012


On Sun, 2012-05-13 at 10:22 +1000, Steven D'Aprano wrote:
> carlo locci wrote:
> > Hello All,
> > I've started to study python a couple of month ago(and I truly love it :)),
> > however I'm having some problems understanding how to modify a sequential
> > script and make it multithreaded (I think it's because I'm not used to
> > think in that way), 
> No, that's because multithreading and parallel processing is hard.

Shared memory multithreading may be hard due to locks, semaphores,
monitors, etc., but concurrency and parallelism need not be hard. Using
processes and message passing, using dataflow, actors or CSP,
parallelism and concurrency is far more straightforward. Not easy,
agreed, but then programming isn't easy.

> > as well as when it's best to use it(some say that
> > because of the GIL I won't get any real benefit from threading my script).
> That depends on what your script does.
> In a nutshell, if your program is limited by CPU processing, then using 
> threads in Python won't help. (There are other things you can do instead, such 
> as launching new Python processes.)

The GIL in Python is a bad thing for parallelism. Using the
multiprocessing package or concurrent.futures gets over the problem.
Well sort of, these processes are a bit heavyweight compared to what can
be achieved on the JVM or with Erlang.

> If your program is limited by disk or network I/O, then there is a possibility 
> you can speed it up with threads.

Or better still use an event based system, cf Twisted.


Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/tutor/attachments/20120513/68d5f866/attachment.pgp>

More information about the Tutor mailing list