Issue
I wrote a Python program that uses threading.Thread
classes. Around 25 threads are launched. It is running on a Raspberry Pi 4 (4 cores).
I was thinking that only 1 Python process would be launched when using threads (because of the GIL). It looks like 35 processes are launched.
top
shows that python3 uses 140% CPU. (only 1 process is showing up)
htop
shows many python3 processes. They also sum up to around 140%.
pstree -p
shows the following (only the python part):
├─python3(28401)─┬─{python3}(28402)
│ ├─{python3}(28403)
│ ├─{python3}(28404)
│ ├─{python3}(28405)
│ ├─{python3}(28406)
│ ├─{python3}(28407)
│ ├─{python3}(28408)
│ ├─{python3}(28409)
│ ├─{python3}(28410)
│ ├─{python3}(28412)
│ ├─{python3}(28413)
│ ├─{python3}(28414)
│ ├─{python3}(28415)
│ ├─{python3}(28416)
│ ├─{python3}(28417)
│ ├─{python3}(28418)
│ ├─{python3}(28419)
│ ├─{python3}(28421)
│ ├─{python3}(28422)
│ ├─{python3}(28423)
│ ├─{python3}(28424)
│ ├─{python3}(28425)
│ ├─{python3}(28426)
│ ├─{python3}(28427)
│ ├─{python3}(28428)
│ ├─{python3}(28429)
│ ├─{python3}(28430)
│ ├─{python3}(28432)
│ ├─{python3}(28433)
│ ├─{python3}(28434)
│ ├─{python3}(28435)
│ ├─{python3}(28437)
│ ├─{python3}(28438)
│ ├─{python3}(28444)
│ └─{python3}(28445)
My question is how to explain this? Are really many processes running in parallel meaning that it would be possible to use up to 300-400% CPU? Or is it some I/O bound artifacts?
Solution
Threads in Linux look like processes to some tools. It's because of the way threads evolved from processes over time. Basically they created "threads," by giving processes the ability to share the same virtual address space, same file handles, etc. In some sense, different "threads" within the same "process" actually are distinct processes with distinct process IDs.
Try pstree --hide-threads
if you don't want to see the threads.
Answered By - Solomon Slow Answer Checked By - David Marino (WPSolving Volunteer)