Threads in Python

Cameron Simpson cs at zip.com.au
Fri Sep 2 00:43:01 CEST 2011


On 01Sep2011 15:27, Stephen Hansen <me+list/python at ixokai.io> wrote:
| On 9/1/11 2:45 PM, George Kovoor wrote:
| > Why doesn't python threads show an associated PID?  On spawning python
| > threads using the threading module I can only see the main thread's pid on
| > using top or ps unix command, no  subprocesses are displayed. In otherwords
| > top or ps in not aware of any subprocesses created using threading module in
| > python.
| >
| > Whereas in Java , creating threads will result in separate pid , these
| > subprocesses can be listed using top or ps. Java threads get mapped to the
| > cores in the system.
| 
| I think you're confused about what threads and subprocesses are. They
| are completely different mechanisms for concurrent code. Threads never
| show up on top or ps, in any language ... or the language isn't offering
| threads. I don't know Java, so I can't really comment on it much, but it
| may be misusing the 'thread' word, but I somehow doubt it. I suspect
| you're just mistaken about what Java is offering.

No, you're mistaken about the threading models on offer.

Some systems offer a threading model where threads can have distinct
process ids; the only real criterion is that they share the same address
space.

The advantages of separate process ids for threads include letting
the OS arrange their scheduling, delivery of signals (on UNIX systems)
to a particular thread, ability to use multiple cores. On the flipside,
threads with distinct process ids tend to be more expensive to set up
and may be more expensive in thread switching.

Java has long shipped with multiple threading implementations; IIRC
"green threads" is an "all in one process id" model that can be used on
any platform.

Some use a mix of heavyweight (threads with distinct pids) and
lightweight threads.

| But, in Python, only one thread actually ever executes actual Python
| code at any given time.

In CPython this is true. Other implementations like Jython can use
other threading models; I'd expect Jython to take a lot of advantage of
Java's native threads.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Seeing my great fault
Through darkening blue windows
I begin again
- Haiku Error Messages http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html



More information about the Python-list mailing list