The threading.Timer extension to return a value from a function gives a TypeError

I tried extending threading.Timer to get return values ​​from a function. I used a solution from this thread and applied modifications (since Timer () already accepts * args and ** kwargs, I don’t think I need to pass it through __init__again). The code looks like this:

from threading import Timer

class CustomTimer(Timer):
    def __init__(self):
        super(CustomTimer, self).__init__()
        self._return = None

    def run(self):
        super(CustomTimer, self).run()
        self._return = self._Thread__target(*self._Thread__args, **self._Thread__kwargs)

    def join(self):
        super(CustomTimer, self).join()
        return self._return

Then I get the following error when starting the main module:

Traceback (most recent call last):
  File "main.py", line 43, in <module>
    from storage import *
  File "/home/mei/tmwAthena/manamarket/storage.py", line 13, in <module>
    from utils import ItemDB
  File "/home/mei/tmwAthena/manamarket/utils.py", line 142, in <module>
    class CustomTimer(Timer):
TypeError: Error when calling the metaclass bases
    function() argument 1 must be code, not str

I don’t understand this error, since the function () is pretty simple, it gets * self.args and ** self.kwargs, and I don’t see where it can look for the string, since the initializer is pretty much the same from the superclass. Please note that this happens when importing a class, but is not even used for code.

+4
1

, , Timer , / Python 2 threading.py:

>>> from threading import Timer
>>> type(Timer)
<type 'function'>

_Timer. , ; init , target, args, kwargs interval .

, CustomTimer:

from threading import _Timer

class CustomTimer(_Timer):
    def __init__(self, interval, function, args=[], kwargs={}):
        self._original_function = function
        super(CustomTimer, self).__init__(
            interval, self._do_execute, args, kwargs)

    def _do_execute(self, *a, **kw):
        self.result = self._original_function(*a, **kw)

    def join(self):
        super(CustomTimer, self).join()
        return self.result

:

def add_together(a, b):
    return a + b

c = CustomTimer(1, add_together, (2, 4))
c.start()
print c.join()

6 1 .

+4

All Articles