psutil
psutil copied to clipboard
Get thread names when using Process.threads()
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.
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.
Windows doesn't have thread names: http://stackoverflow.com/questions/9366722/how-to-get-the-name-of-a-win32-thread
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
Sorry to "necrobump" this, but are there any reasons why these commits have not been merged on master ?
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. ;)
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 ?
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.
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.
I confirm the same test crashes on master
Just FYI I won't have time to look into this any time soon, sorry.
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