Home > Enterprise >  Heroku bot AttributeError: thread object has no attribute 'isAlive'
Heroku bot AttributeError: thread object has no attribute 'isAlive'

Time:07-10

Recently, I followed a tutorial on how to make a timeout function for my discord.py bot, so one of the commands wont take the bot too long. The command uses another script file, which is imported at the top of the bot.py code. When I ran the bot locally with py bot.py, the timeout works. But when I host the bot on Heroku, the command doesn't work. I looked to see if there was an error and used heroku run bash and then python bot.py and it produced this error when I typed the command on Discord:

Ignoring exception in command calculator:
Traceback (most recent call last):
(traceback...)

AttributeError: 'InterruptableThread' object has no attribute 'isAlive'

The above exception was the direct cause of the following exception:
(more traceback...)

The timeout function uses threading library. Here was the code for the timeout function:

def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
    class InterruptableThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.result = None

        def run(self):
            try:
                self.result = func(*args, **kwargs)
            except:
                self.result = default

    it = InterruptableThread()
    it.start()
    it.join(timeout_duration)
    if it.isAlive():
        raise TimeLimitExpired()
    else:
        return it.result

I got this from the tutorial. I use the timeout function by doing

timeout(interpreter.visit, (ast.node, context), timeout_duration=TIMEOUT, 
default=RuntimeResult().failure("Execution timed out."))

and the variable TIMEOUT is 5. I am using Windows 10 and Python from the Windows Store.

CodePudding user response:

it is an object of InterruptableThread, which inherits from threading.Thread. The Thread class does not have any attribute named isAlive, instead it has one called is_alive.

Change it.isAlive to it.is_alive.

Beware that python method names usually use snake_case and uses PascalCase for class names.

  • Related