我现在正在和Python一起工作。我有一个start函数,它从消息中获取一个字符串。我想为每条消息启动线程。
现在的线索应该是这样打印出我的信息:
def startSuggestworker(message):
print(message)
def start():
while True:
response = queue.receive_messages()
try:
message = response.pop()
start_keyword = message.body
t = threading.Thread(target=startSuggestworker, args = (start_keyword))
t.start()
message.delete()
except IndexError:
print("Messages empty")
sleep(150)
start()
现在我得到了一个TypeError
并且不明白为什么。异常消息如下:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
TypeError: startSuggestworker() takes 1 positional argument but y were given
*y=我的绳子的长度
我做错什么了?
threading.Thread
的args
kwarg需要一个iterable,该iterable中的每个元素都被传递给目标函数。因为您为
args
提供了一个字符串:t = threading.Thread(target=startSuggestworker, args=(start_keyword))
每个字符作为单独的参数传递给
startSuggestworker
。相反,您应该提供
args
元组:相关问题 更多 >
编程相关推荐