项目列表中的Multie Workers

时间:2016-07-14 23:16:54

标签: python multithreading

我正在尝试生成多个线程,每个线程都调用一个处理列表中项目的函数。下面的插图,但我收到以下错误,我不完全确定为什么TypeError: stuff() takes exactly 1 argument (56 given)。以下代码是测试该想法功能的片段。在继续之前,我将检查每个线程是否仍然处于活动状态(这将在以后一旦错误的原因被ID)。任何帮助都会被贬低。我相信线程将参数作为列表,这可能是原因(文件名被拆分)但我不知道如何列出列表(列表中的每个项目一个)问题。如果您无法将该项解析为该函数。

def stuff(filename):
    Stuff done to filename


threads = []
for filename in file_list: #File list contains list of full path filenames.
    thread1 = threading.Thread(target=stuff, args=(filename))
    threads.append(thread1)
    thread1.start()

1 个答案:

答案 0 :(得分:2)

您应该将元组传递给args。没有逗号,,它不是一个元组 - 它只是一个带括号的简单表达式。

thread1 = threading.Thread(target=stuff, args=(filename,))
                                                       ^^^