Python thread

Christian Heimes lists at cheimes.de
Fri Sep 2 01:09:51 CEST 2011


Am 02.09.2011 00:46, schrieb Benjamin Kaplan:
> Threading is an OS-level construct to allow concurrency within a
> single process (and address space). Threads are never supposed to be
> separate processes (they aren't at the C-level, so I don't know what
> Java is doing here). CPython code has a global interpreter lock which
> prevents two threads from running Python code at the same time, but
> they're still useful for asynchronous operations. For example, one
> thread can be waiting for user input while another thread continues to
> process data. Other Python implementations such as Jython and
> IronPython don't have a global interpreter lock so threads can run
> concurrently (and on different cores in a multi-core machine).

On Linux threading is implemented with multiple processes. A Linux
pthread is a clone of the process created with the clone(2) syscall. [1]
Each thread has a PID and an entry in the kernel's process table. Tools
like htop can show user land threads as different processes. This may
explain the confusion of the OP. He may have seen multiple Java threads
as different processes.

psutil can list all threads with PIDs. The getpid(2) syscall returns
always the PID of the main process, gettid (only available through
syscall(SYS_gettid)) returns the PID of the current thread.

Christian

[1] http://linux.die.net/man/2/clone




More information about the Python-list mailing list