O'Reilly logo

Python Parallel Programming Cookbook by Giancarlo Zaccone

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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()+\ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required