Skip to content

Thread methods changed in Python 3.9 and newer versions #6

Open
@Matej-Chmel

Description

@Matej-Chmel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions