2015-10-06 29 views
0

我学习在Python的线程和并发的一部分,我选择,我做了一个os.walk()获取某个目录中的文件列表为例,填充到一个数组使用os.path.join(),然后使用线程来更改这些文件的所有权。这个脚本的目的是学习线程。我的代码是Python的线程具有可变参数(Python的2.4.3)

for root, dir, file in os.walk("/tmpdir/"): 
    for name in file: 
     files.append(os.path.join(root, name)) 

def filestat(file): 
    print file ##The code to chown will go here. Writing it to just print the file for now. 

thread = [threading.Thread(target=filestat, args="filename") for x in range(len(files))] 
print thread ##This will give me the total number of thread objects that is created 
for t in thread: 
    t.start() ##This will start the thread execution 

len(files)次执行这将打印在“文件名”。但是,我想将列表文件中的文件名作为参数传递给函数。我该怎么办?

回答

1

你应该用你遍历args参数里面的变量名。不要忘记让它成为一个元组。

thread = [threading.Thread(target=filestat, args=(files[x],)) for x in range(len(files))] 

或者

thread = [threading.Thread(target=filestat, args=(filename,)) for filename in files] 
+0

谢谢凯文。那么这个程序中使用的线程数或并发数是多少? – pkill

+0

每个文件名一个。 – Kevin

+0

如果我想将并发性提高到'x'数字,我应该在这个程序中修改什么? – pkill