Mastering-Concurrency-in-Python icon indicating copy to clipboard operation
Mastering-Concurrency-in-Python copied to clipboard

Thread methods changed in Python 3.9 and newer versions

Open Matej-Chmel opened this issue 2 years ago • 0 comments

When running the file Chapter05/example6.py in Python 3.9 or newer, this error is raised:

AttributeError: 'MyThread' object has no attribute 'isAlive'. Did you mean: 'is_alive'?

This can be fixed by renaming the method at line 21.

alive = [1 if thread.isAlive() else 0 for thread in threads] # original
alive = [1 if thread.is_alive() else 0 for thread in threads] # solution

Also when running the script under Python 3.10 or newer, a warning is emitted:

DeprecationWarning: setDaemon() is deprecated, set the daemon attribute instead

The issue can be fixed by changing the line 41 like so:

thread.setDaemon(True) # original
thread.daemon = True # solution

The method threading.currentThread was also deprecated in favor of threading.current_thread. When running scripts Chapter12/example3.py and Chapter12/example4.py a warning will be emitted:

DeprecationWarning: currentThread() is deprecated, use current_thread() instead

Interestingly, in chapter 13, the correct method threading.current_thread is used.

Matej-Chmel avatar Aug 31 '23 21:08 Matej-Chmel