Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15780

Re: Multiple threads

Date 2011-11-16 12:27 -0500
From Dave Angel <d@davea.name>
Subject Re: Multiple threads
References <31766634.4.1321451296410.JavaMail.geo-discussion-forums@yqcm23> <CAPTjJmoAWni4-wyKB4HgAJ5KvTbz42_BC3f22MOnugUmmXzVtA@mail.gmail.com> <CAAdqOWF=irJXFO2q2+o8jpM09GMLf8N+NTNGTOtMhwKgtG6jag@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2775.1321464507.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 11/16/2011 12:00 PM, Jack Keegan wrote:
> Hi Chris,
>
> On Wed, Nov 16, 2011 at 1:55 PM, Chris Angelico<rosuav@gmail.com>  wrote:
>
>> First off, it's better in CPython (the most popular Python) to use
>> multiple processes than multiple threads.
>
> I had been looking into treads and process/subprocess myself a while ago
> and couldn't decide which would suit what I needed to do best. I'm still
> very confused about the whole thing. Can you elaborate on the above a bit
> please?
>
> Cheers,
>
> Jack
Threads and processes are a concept that exists in your operating 
system, and Python can use either of them to advantage, depending on the 
problem.  Note that different OS also handle them differently, so code 
that's optimal on one system might not be as optimal on another.  Still, 
some generalities can be made.

Each process is a separate program, with its own address space and its 
own file handles, etc.  You can examine them separately with task 
manager, for example.  If you launch multiple processes, they might not 
even all have to be python, so if one problem can be handled by an 
existing program, just run it as a separate process.   Processes are 
generally very protected from each other, and the OS is generally better 
at scheduling them than it is at scheduling threads within a single 
process.  If you have multiple cores, the processes can really run 
simultaneously, frequently with very small overhead.  The downside is 
that you cannot share variables between processes without extra work, so 
if the two tasks are very interdependent, it's more of a pain to use 
separate processes.

Within one process, you can have multiple threads.  On some OS, and in 
some languages, this can be extremely efficient.  Some programs launch 
hundreds of threads, and use them to advantage.  By default, it's easy 
to share data between threads, since they're in the same address space.  
But the downsides are 1) it's very easy to trash another thread by 
walking on its variables.  2) Python does a lousy job of letting threads 
work independently.  For CPU-bound tasks, using separate threads is 
likely to be slower than just doing it all in one thread.

-- 

DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Multiple threads Eduardo Oliva <dutche@gmail.com> - 2011-11-16 05:48 -0800
  Re: Multiple threads Chris Angelico <rosuav@gmail.com> - 2011-11-17 00:55 +1100
  Re: Multiple threads Henrik Faber <hfaber@invalid.net> - 2011-11-16 15:07 +0100
  Re: Multiple threads Christian Heimes <lists@cheimes.de> - 2011-11-16 16:01 +0100
  Re: Multiple threads Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-11-16 17:45 +0100
    Re: Multiple threads Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-11-16 16:38 -0800
  Re: Multiple threads Dave Angel <d@davea.name> - 2011-11-16 12:27 -0500
  Re: Multiple threads Michael Hunter <tahoemph@gmail.com> - 2011-11-16 09:55 -0800
  Re: Multiple threads Dave Angel <d@davea.name> - 2011-11-16 13:06 -0500
  Re: Multiple threads Dave Angel <d@davea.name> - 2011-11-16 13:30 -0500
  Re: Multiple threads Miki Tebeka <miki.tebeka@gmail.com> - 2011-11-16 12:50 -0800

csiph-web