0

How can one instrospect to receive the current thread object?
Consider this somewhat artificial code snippet. The use case is different, but for the sake of simplicity, I've boiled it down the the essential bit

t1 = threading.Thread(target=func)
t2 = threading.Thread(target=func)

marked_thread_for_cancellation = t1

t1.start()
t2.start()

def func():
    if [get_thread_obj] is marked_thread_for_cancellation:   # <== introspect here
        return
    # do something
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
  • threading.currentThread() gives you the current thread, or do you need more than that? Threads local data can be used to mark threads. (Maybe I have misunderstood your intention) – Roman Susi Sep 26 '13 at 15:17

2 Answers2

1

You can use thread.get_ident function. Compare thread.get_ident() with Thread.ident as follow:

import thread
import threading
import time

marked_thread_for_cancellation = None

def func(identifier):
    while threading.get_ident() != marked_thread_for_cancellation:
        time.sleep(1)
        print('{} is alive'.format(identifier))
    print('{} is dead'.format(identifier))

t1 = threading.Thread(target=func, args=(1,))
t2 = threading.Thread(target=func, args=(2,))
t1.start()
t2.start()
time.sleep(2)
marked_thread_for_cancellation = t1.ident # Stop t1

In Python 3, use threading.get_ident.

You can also use your own identifier instead of thread.get_ident:

import threading
import time

marked_thread_for_cancellation = None

def func(identifier):
    while identifier != marked_thread_for_cancellation:
        time.sleep(1)
        print('{} is alive'.format(identifier))
    print('{} is dead'.format(identifier))

t1 = threading.Thread(target=func, args=(1,))
t2 = threading.Thread(target=func, args=(2,))
t1.start()
t2.start()
time.sleep(2)
marked_thread_for_cancellation = 1 # Stop t1 (`1` is the identifier for t1)
falsetru
  • 357,413
  • 63
  • 732
  • 636
1

To make minimal changes to your code, here is probably what you are after:

import threading

def func():
    if threading.current_thread() is marked_thread_for_cancellation:   # <== introspect here
        print 'cancel'
    else:
        print 'otherwise'

t1 = threading.Thread(target=func)
t2 = threading.Thread(target=func)

marked_thread_for_cancellation = t1

t1.start()
t2.start()

But I do not understand what do you mean by introspection. marked_thread_for_cancellation is shared by all threads, all threads have by their own is some local data, accessible via threading.local().

Roman Susi
  • 4,135
  • 2
  • 32
  • 47
  • "In computer programming, introspection refers to the ability to examine something to determine what it is, what it knows, and what it is capable of doing." ([source](http://www.ibm.com/developerworks/library/l-pyint/index.html)). In our case the code is introspecting the very thread it is running in. – Jonathan Livni Sep 26 '13 at 17:07
  • 1
    This is too broad a definition. Almost any program may be using introspection then. May be reflection is better term for what I understood by introspection: https://en.wikipedia.org/wiki/Reflection_%28computer_programming%29 but still accessing thread object can hardly be called introspection/reflection... – Roman Susi Sep 26 '13 at 19:24