【发布时间】:2014-07-23 10:10:01
【问题描述】:
我是 python 新手,我不知道为什么“Without_thread”类有效,而不是“With_thread”类。
我的“With_thread”类的目的是在我用函数调用它时启动一个新线程,这样我就可以同时执行任何函数。 (
import threading
class With_thread(threading.Thread):
def __init__(self, target, *args):
self._target = target
self._args = args
threading.Thread.__init__(self)
def run(self):
self._target(*self._args)
“Without_thread”几乎是同一个类,唯一改变的是我不使用线程。
class Without_thread():
def __init__(self, target, *args):
self._target = target
self._args = args
def foo(self):
self._target(*self._args)
我用这个测试我的代码:
def some_Func(data, key):
print("some_Func was called : data=%s; key=%s" % (str(data), str(key)))
f1 = With_thread(some_Func, [1,2], 6)
f1.start()
f1.join()
f2 = Without_thread(some_Func, [1,2], 6)
f2.foo()
f1 的输出是:
self._target(*self._args)
TypeError: 'NoneType' object is not callable
f2 的输出是:
some_Func was called : data=[1, 2]; key=6
如果您能帮我解决这个问题,我将不胜感激,非常感谢您抽出宝贵时间!!!
【问题讨论】:
标签: python multithreading function-parameter