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

store thread.get_ident() thread identifier inside threading.Thread objects #47120

Closed
irmen mannequin opened this issue May 15, 2008 · 7 comments
Closed

store thread.get_ident() thread identifier inside threading.Thread objects #47120

irmen mannequin opened this issue May 15, 2008 · 7 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@irmen
Copy link
Mannequin

irmen mannequin commented May 15, 2008

BPO 2871
Nosy @gpshead, @irmen, @pitrou

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/gpshead'
closed_at = <Date 2008-06-01.23:50:16.481>
created_at = <Date 2008-05-15.21:49:54.593>
labels = ['extension-modules', 'type-feature']
title = 'store thread.get_ident() thread identifier inside threading.Thread objects'
updated_at = <Date 2008-06-01.23:50:16.480>
user = 'https://github.com/irmen'

bugs.python.org fields:

activity = <Date 2008-06-01.23:50:16.480>
actor = 'gregory.p.smith'
assignee = 'gregory.p.smith'
closed = True
closed_date = <Date 2008-06-01.23:50:16.481>
closer = 'gregory.p.smith'
components = ['Extension Modules']
creation = <Date 2008-05-15.21:49:54.593>
creator = 'irmen'
dependencies = []
files = []
hgrepos = []
issue_num = 2871
keywords = ['patch']
message_count = 7.0
messages = ['66882', '67248', '67274', '67310', '67312', '67314', '67619']
nosy_count = 3.0
nosy_names = ['gregory.p.smith', 'irmen', 'pitrou']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue2871'
versions = []

@irmen
Copy link
Mannequin Author

irmen mannequin commented May 15, 2008

I've ran into a problem where it would be very nice to be able to tell
the tread.get_ident() of a given threading.Thread object.

Currently, when creating a new Thread object, there is no good way of
getting that thread's get_ident() value.

I propose adding the get_ident() value as a publicly accessible field of
every threading.Thread object.

@irmen irmen mannequin added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels May 15, 2008
@pitrou
Copy link
Member

pitrou commented May 23, 2008

Currently, when creating a new Thread object, there is no good way of
getting that thread's get_ident() value.

Well, how about doing it at the beginning of its run() method, e.g. in a
Thread subclass:

    class MyThread(threading.Thread):
        def run(self):
            self.thread_ident = thread.get_ident()
            threading.Thread.run(self) # or any other stuff

Also, I don't think get_ident() is often useful when using the Threading
module, since you can just use the id() of the current Thread object
instead.

@irmen
Copy link
Mannequin Author

irmen mannequin commented May 23, 2008

Adding it in the run method would only work for threads that I create in
my own code. The thing is: I need to be able to get the tread
identification from threads created by third party code. So I cannot
rely on that code putting it in the thread object themselves like that.
Hence my wish of letting the standard library module take care of it.

And using the id() of the current thread object has a rather obscure
problem. I was using it as a matter of fact, until people reported
problems in my code when used with certain atexit handling. (Sometimes
the wrong id() is returned). Because of that I wanted to switch to the
more low-level thread.get_ident() identification of different threads,
because that is supposed to return a stable os-level thread
identification, right?

@pitrou
Copy link
Member

pitrou commented May 24, 2008

Well, it's true that get_ident() will always give you a reliable number
while currentThread() can play dirty games on you at shutdown. The thing
is the Thread object is dereferenced before the OS thread actually
terminates, so it may be that a Python callback is called by some C code
in those last moments and gets the wrong answer from currentThread()
(because currentThread() returns a new dummy thread when it can't find a
Thread object corresponding to the get_ident() value).

So finally your request sounds quite reasonable :-)

@gpshead
Copy link
Member

gpshead commented May 24, 2008

This seems like a useful addition and is easy enough.

Patch with tests and documentation attached. Unless I hear objections I
will commit it to trunk later this week.

Patch available for review here: http://codereview.appspot.com/1301

@gpshead gpshead self-assigned this May 24, 2008
@irmen
Copy link
Mannequin Author

irmen mannequin commented May 24, 2008

Thanks Gregory, for taking the time to make a patch.

@gpshead
Copy link
Member

gpshead commented Jun 1, 2008

committed to trunk r63882 for inclusion in 2.6.

@gpshead gpshead closed this as completed Jun 1, 2008
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants