简体   繁体   English

扩展threading.Timer以从函数返回值给出TypeError

[英]Extending threading.Timer for returning value from function gives TypeError

I tried to extend threading.Timer so I can get return values from the function. 我试图扩展threading.Timer,以便可以从函数中获取返回值。 I used the solution from this thread and applied the modifications (as Timer() already takes *args and **kwargs I don't think I need to pass it through __init__ again). 我使用了该线程的解决方案并进行了修改(因为Timer()已经带有* args和** kwargs了,我认为我不需要再次将其传递给__init__ )。 Code goes exactly 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 running 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 function() is quite simple, it receives *self.args and **self.kwargs and I can't see where it could be looking for a string since the initializator is pretty much the same from superclass. 我不明白这个错误,因为function()非常简单,它接收到* self.args和** self.kwargs,并且由于初始化器与超类。 Note that this is occurring while importing the class, it's not even getting to be used by the code. 请注意,这是在导入类时发生的,甚至没有被代码使用。

The error you're getting is because Timer is not a class, it is a function/method in Python 2 threading.py : 您收到的错误是因为Timer不是类,而是Python 2 threading.py函数/方法

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

The concrete class is _Timer . 具体的类是_Timer Furthermore your code does have some problems; 此外,您的代码确实存在一些问题。 your init is missing arguments and the target , args , kwargs and interval are not set. 您的init缺少参数,并且未设置targetargskwargsinterval

You might have luck with this CustomTimer : 您可能对此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

Demo: 演示:

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

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

prints 6 after 1 second. 1秒后打印6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM