【发布时间】:2009-03-19 03:26:56
【问题描述】:
鉴于Python documentation 对应于Thread.run():
您可以在子类中覆盖此方法。标准 run() 方法调用作为目标参数传递给对象构造函数的可调用对象,如果有的话,顺序参数和关键字参数分别取自 args 和 kwargs 参数。
我已经构建了以下代码:
class DestinationThread(threading.Thread):
def run(self, name, config):
print 'In thread'
thread = DestinationThread(args = (destination_name, destination_config))
thread.start()
但是当我执行它时,我收到以下错误:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
TypeError: run() takes exactly 3 arguments (1 given)
我似乎遗漏了一些明显的东西,但我看到的各种例子都适用于这种方法。最终,我试图将字符串和字典传递到线程中,如果构造函数不是正确的方法,而是在启动线程之前创建一个新函数来设置值,我对此持开放态度。
关于如何最好地完成此任务的任何建议?
【问题讨论】:
-
像我这样的读者可能会发现this帖子直接回答了PO的问题,而选择的答案提供了更好的实用选择。
标签: python multithreading