Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the current thread ID #418

Open
giampaolo opened this issue May 23, 2014 · 9 comments
Open

Get the current thread ID #418

giampaolo opened this issue May 23, 2014 · 9 comments

Comments

@giampaolo
Copy link
Owner

From kunalpar...@gmail.com on August 09, 2013 12:33:00

Proposal: add a function to get the ID of the calling thread.
 Patch attached

Attachment: current_thread_id.patch

Original issue: http://code.google.com/p/psutil/issues/detail?id=418

@giampaolo
Copy link
Owner Author

From g.rodola on August 09, 2013 03:41:19

Isn't this the same as:

>>> import threading
>>> threading.current_thread()
<_MainThread(MainThread, started 139944996378368)>
>>> threading.current_thread().ident
139944996378368
>>> 

...?

@giampaolo
Copy link
Owner Author

From kunalpar...@gmail.com on August 09, 2013 11:00:55

It is the same for Windows, but not for POSIX systems.

threading assigns each thread an ID that is retrieved using pthread_self (Ref: 
http://hg.python.org/cpython/file/0152152b09d0/Python/thread_pthread.h#l226 ). 
pthread_self return type is pthread_id which is an opaque type. So the value 
you see from threading.current_thread is actually the opaque type returned by 
pthread_self which turns out is actually the address of a struct.

Now that works fine if all you need is to differentiate between threads. But 
I'm trying to do some performance analysis for my application. The tools like 
Instruments or Activity Monitor on OS X report the actual thread ID. This 
functions helps tie the threads reported in the tools to the thread in python.

$] python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> psutil.get_current_thread_id()
5444082
>>> import threading
>>> threading.current_thread()
<_MainThread(MainThread, started 140735314661760)>
>>> hex(140735314661760)
'0x7fff7e701180'

@giampaolo
Copy link
Owner Author

From g.rodola on August 09, 2013 12:10:54

Oh I see, the use case would be being able to map current thread's ID with one 
of the items returned by Process.get_threads() right?

Makes sense I'd say, even though Process.get_threads() should probably provide 
more info other than just id and CPU timings ("name" or even "memory" on those 
platforms where this can be retrieved).

Process.get_threads() might also grow an extra "id" parameter so that it can be 
used as such:

>>> p = psutil.Process(pid)
>>> p.get_threads(id=psutil.get_current_thread_id())
thread(id=2539, user_time=0.03, system_time=0.02)
>>>

(note: this leads to questions such as "should we have a custom NoSuchThread 
exception?")

Generally speaking I like the idea, but issue 224 should be fixed first.

@giampaolo
Copy link
Owner Author

From kunalpar...@gmail.com on August 22, 2013 23:04:30

Yes, that is correct.

This change is independent of the change reported for issue 224 . Though it 
would be nice to get issue 224 fixed as well. Whats needed to fix issue 224 ?

@giampaolo
Copy link
Owner Author

From g.rodola on August 24, 2013 02:21:19

It's not independent from issue 224 . 
If on OSX IDs returned by get_threads() are fake then you can't map them with 
p.get_threads(id=psutil.get_current_thread_id()).

@nikhilm
Copy link
Contributor

nikhilm commented May 14, 2018

Is there any plan to revive this by dropping support for old OSes now that 10.6 is really old and unsupported even by Apple?

@lsmithso
Copy link

I could really use this proposal for monitoring per thread CPU
utilization. Any chance of some movement on this?

Thanks.

@monicatao
Copy link

I am also looking for the solution to this problem. any update?

@JanKanis
Copy link

I used int(os.readlink('/proc/thread-self').split('/')[-1]) as a workaround. Works on Linux assuming /proc is mounted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants