So here's what I'm seeing. I run some code on my MacOS laptop that will start threads (jump down to see python code).
I get output like the following:
Starting the task 1...
Starting the task 2...
Starting the task 3...
Starting the task 4...
Starting the task 5...
Starting the task 6...
Starting the task 7...
Starting the task 8...
Starting the task 9...
Starting the task 10...
pid: 73257
number of threads: 11
Then, go to another tab, run htop or pstree | grep python or ps -efT | grep python, and for some reason I don't see any additional threads underneath my python process:
ps -efT | grep python:
502 69605 60667 0 8:52PM ttys009 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox python
502 69211 62472 0 8:52PM ttys011 0:00.05 python example-threading.py
htop -p 73257:
htop configuration:
pstree | grep python:
=.tox python
| | \--= 78847 brian.peterson python example-threading.py
| | |--- 01029 brian.peterson /Users/brian.peterson/other-program.py
This is at odds with what I see everyone else reporting in stackoverflow questions about htop and threads. They are confused why htop reports more than 1 process, but I am expecting it to report more, and am using it because I am aware that it should.
I tried some other executables that I expect to start threads, and I am also not seeing threads show up in those cases, so I am no longer thinking this is python-related.
I have had success viewing threads like this on other systems, maybe I just never tried on my own laptop before.
Code:
import os
import threading
from time import sleep, perf_counter
from threading import Thread
def task(id):
print(f'Starting the task {id}...')
sleep(60)
print(f'The task {id} completed')
start_time = perf_counter()
# create and start 10 threads
threads = []
for n in range(1, 11):
t = Thread(target=task, args=(n,))
threads.append(t)
t.start()
# wait for the threads to complete
pid = os.getpid()
print(f"pid: {pid}")
print(f"number of threads: {threading.active_count()}")
for t in threads:
t.join()
end_time = perf_counter()
print(f'It took {end_time- start_time: 0.2f} second(s) to complete.')

