PacktPublishing / Mastering-Concurrency-in-Python

Mastering Concurrency in Python, published by Packt
MIT License
109 stars 67 forks source link

Thread methods changed in Python 3.9 and newer versions #6

Open Matej-Chmel opened 1 year ago

Matej-Chmel commented 1 year ago

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.