How to determine the current thread

Using arguments to identify or name the thread is cumbersome and unnecessary. Each Thread instance has a name with a default value that can be changed as the thread is created. Naming threads is useful in server processes with multiple service threads that handle different operations.

How to do it…

To determine which thread is running, we create three target functions and import the time module to introduce a suspend execution of two seconds:

import threading import time def first_function(): print (threading.currentThread().getName()+\ str(' is Starting \n')) time.sleep(2) print (threading.currentThread().getName()+\ str( ' is Exiting \n')) return def second_function(): print (threading.currentThread().getName()+\ ...

Get Python Parallel Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.