在Python中将字典传递给线程函数

在Python中将字典传递给线程函数,python,multithreading,dictionary,Python,Multithreading,Dictionary,所以,我有一个函数explain(item),它接受一个参数。此参数旨在成为具有8-9个键的词典。当我打电话给explain(项目)时,一切都很好。但是当我调用时(items是相同的变量) 我会遇到这样的错误: Exception in thread Thread-72: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/thread

所以,我有一个函数explain(item),它接受一个参数。此参数旨在成为具有8-9个键的词典。当我打电话给
explain(项目)
时,一切都很好。但是当我调用时(items是相同的变量)

我会遇到这样的错误:

Exception in thread Thread-72:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: explain() takes exactly 1 argument (10 given)

我做错了什么?

看起来您打算将一个一项元组作为
args
参数传递给
threading.Thread()
,但是使用
args=(item)
相当于
args=item
。您需要添加一个逗号来创建元组,因此它将是
args=(item,)

不带尾随逗号的括号只是对表达式进行分组的方法,例如:

>>> (100)  # this is just the value 100
100
>>> (100,) # this is a one-element tuple that contains 100
(100,)

self.\uu-target(*self.\uu-args,**self.\uu-kwargs)
可能应该是
self.\uu-target(self.\uu-args,self.\uu-kwargs)
。否则,您将把字典中的每一项都作为参数传递。
self.\u target
调用来自线程模块的源代码,肯定不是问题所在!
threads.append(threading.Thread(target = explain, args=(item,))).
>>> (100)  # this is just the value 100
100
>>> (100,) # this is a one-element tuple that contains 100
(100,)