psutil icon indicating copy to clipboard operation
psutil copied to clipboard

Get thread names when using Process.threads()

Open bleedinge opened this issue 9 years ago • 11 comments

Since that information is already in the /proc filesystem stat file, it would be great if the tuple returned by threads() could contain the thread name (2nd parameter in the stat file) next to the pid, utime and stime.

bleedinge avatar Mar 31 '15 15:03 bleedinge

Yes, this is something I've been wanting to do for a while. It's likely we will not able to retrieve name an all platforms but since it's an important data I think it's worth it to return it anyway on those platforms supporting it.

giampaolo avatar Jul 10 '15 00:07 giampaolo

Windows doesn't have thread names: http://stackoverflow.com/questions/9366722/how-to-get-the-name-of-a-win32-thread

giampaolo avatar Sep 03 '15 08:09 giampaolo

Patch for OSX available at: https://code.google.com/p/plcrashreporter/issues/detail?id=65 Sample code: https://github.com/janmojzis/pstree/blob/master/proc_kvm.c

giampaolo avatar Dec 14 '15 18:12 giampaolo

Sorry to "necrobump" this, but are there any reasons why these commits have not been merged on master ?

fthiery avatar Sep 25 '17 09:09 fthiery

There are no "commits", just some research I did back then. I ended up forgetting about this ticket because I don't consider it too important but any PR is welcome. ;)

giampaolo avatar Sep 25 '17 16:09 giampaolo

Well, there's a branch :) I could start from that point, do you remember what was left ? Some testing maybe ? Or does this not merge anymore ?

fthiery avatar Sep 26 '17 10:09 fthiery

Oh! I totally forgot there was a branch, sorry. It seems back then I added the implementation for Linux, FreeBSD and OpenBSD. In order to revamp this issue we should 1) rebase from the current master and 2) add the OSX implementation, then it would be reasonably good to go.

giampaolo avatar Sep 26 '17 10:09 giampaolo

Did my best: https://github.com/giampaolo/psutil/pull/1135

The tests work (although one of them fails -- i'm not sure it is related though):

$ python setup.py test
[...]
Ran 506 tests in 5.397s

FAILED (failures=3, errors=14, skipped=148)
Test failed: <unittest.runner.TextTestResult run=506 errors=14 failures=3>
error: Test failed: <unittest.runner.TextTestResult run=506 errors=14 failures=3>
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/home/fthiery/src/external/psutil/psutil/tests/__init__.py", line 199, in _cleanup_procs
    reap_children(recursive=True)
  File "/home/fthiery/src/external/psutil/psutil/tests/__init__.py", line 441, in reap_children
    children = set(psutil.Process().children(recursive=True))
  File "/home/fthiery/src/external/psutil/psutil/__init__.py", line 392, in __init__
    self._init(pid)
  File "/home/fthiery/src/external/psutil/psutil/__init__.py", line 414, in _init
    self._proc = _psplatform.Process(pid)
  File "/home/fthiery/src/external/psutil/psutil/_pslinux.py", line 1392, in __init__
    self._procfs_path = get_procfs_path()
  File "/home/fthiery/src/external/psutil/psutil/_pslinux.py", line 217, in get_procfs_path
    return sys.modules['psutil'].PROCFS_PATH
KeyError: 'psutil'

Is OSX really supposed to support naming threads ?

Btw i tested it manually against my use case on Linux and it worked.

fthiery avatar Sep 26 '17 12:09 fthiery

I confirm the same test crashes on master

fthiery avatar Sep 26 '17 12:09 fthiery

Just FYI I won't have time to look into this any time soon, sorry.

giampaolo avatar Oct 06 '17 05:10 giampaolo

Hello, any update on this? I tried to retrieve thread names using: threading._active.get(id) but it seems like the id returned for each thread by Process.threads() is not the correct id.

Example:

>>> for p in threading.enumerate():
>>>     print(p.ident)
  
4407995904
123145397399552
123145402654720
123145408983040
123145414238208
>>> for t in proc.threads():
>>>     print(t.id)
    
1
2
3
4
5
6

mfoglio avatar Sep 29 '21 15:09 mfoglio