shabab ashfaque
shabab ashfaque

Reputation: 65

Retrieving thread id from a thread object before starting the thread in python

Is there any way to receive a thread id from a thread object before actually starting the thread? For example:

 t = threading.Thread(
                name=...
                target=...
                args=...
)

# I want the id here

t.start()

Is that possible or is there any other workaround for this?

Thanks

Upvotes: 0

Views: 4209

Answers (2)

As I commented, the documentation states clearly that ident (and native_id) is None before the thread has started. See https://docs.python.org/3/library/threading.html?highlight=ident#threading.Thread.ident

Here's a way you can get the ident or native_id before the thread has done anything useful, using a semaphore which the main code acquires before starting the thread, and releases once main is ready for the thread to continue.

import threading
import time

def threadfunc(word1,word2):
    global s
    print( f"Thread Started" )
    print( f"Thread Acquiring sempahore" )
    s.acquire()
    print( f"Thread Acquired sempahore" )
    print( f"{word1}, {word2}" )
    # do something useful ....
    time.sleep(0.1)
    # wrap-up
    print( f"Thread Releasing sempahore" )
    s.release()
    print( f"Thread Released sempahore" )
    print( "Thread Finished" )

s = threading.Semaphore()

print( f"Main Acquiring sempahore" )
s.acquire()
print( f"Main Acquired sempahore" )

t = threading.Thread(
                name='mythread'
                ,target=threadfunc
                ,args=("hello","world")
    )

# I want the id here
id = t.ident

print( f"Thread id before start: {id}" )

print( "Main starting thread" )
t.start()
print( "Main started thread" )

id1 = t.ident

print( f"Thread id after start: {id1}" )

print( f"Main Releasing sempahore" )
s.release()
print( f"Main Released sempahore" )

t.join()
print( "Main Finished" )

Result:

Main Acquiring sempahore
Main Acquired sempahore
Thread id before start: None
Main starting thread
Thread Started
Main started thread
Thread Acquiring sempahore
Thread id after start: 16932
Main Releasing sempahore
Main Released sempahore
Thread Acquired sempahore
hello, world
Thread Releasing sempahore
Thread Released sempahore
Thread Finished
Main Finished

Semaphore documentation is here https://docs.python.org/3/library/threading.html?highlight=semaphore#threading.Semaphore

Other synchronization methods are available, e.g. Lock, RLock, conditions, etc, and could easily be used instead of Sempahore - all are on the doc link above.

Upvotes: 1

Stephen
Stephen

Reputation: 1518

Unfortunately this is not possible. Thread ID's are retrieved using the native_id member on a Thread object and are only available with a valid value after the thread has been started. From the Python docs:

The native integral thread ID of this thread. This is a non-negative integer, or None if the thread has not been started. See the get_native_id() function. This represents the Thread ID (TID) as assigned to the thread by the OS (kernel). Its value may be used to uniquely identify this particular thread system-wide (until the thread terminates, after which the value may be recycled by the OS).

Upvotes: 1

Related Questions