我有这段代码:
import multiprocessing as mp
import shutil
import md5
def f(src,dst):
shutil.copy2(src,dst)
file_1 = "C:\Users\Nick\Documents\production\TEST\\test.txt"
file_2 = "C:\Users\Nick\Documents\production\TEST_2\\test.txt"
def get_md5(file_name):
with open(file_name) as file_to_check:
# read contents of the file
data = file_to_check.read()
# pipe contents of the file through
md5_returned = md5.new(data).hexdigest()
print md5_returned
if __name__ == '__main__':
P = mp.Process(target=f, args=(file_1,file_2))
s = mp.Process(target=get_md5, args=(file_1))
P.start()
P.join()
s.start()
s.join()
我现在只测试如何使用多处理,但get_md5函数会抛出类型错误。 错误信息是这样的:
Traceback (most recent call last):
File "C:\Python27\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "C:\Python27\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
TypeError: get_md5() takes exactly 1 argument (48 given)
在我看来,好像get_md5进程只有一个参数,我不知道48个参数来自哪里。
有人可以帮忙吗?
答案 0 :(得分:8)
我认为,你必须将args作为元组传递:
s = mp.Process(target=get_md5, args=(file_1,))
你错过了逗号。如果您错过了逗号,则将file_1中的各个字符视为单独的参数。
编辑:
包括我认为适合上下文的Adam Smith's响应:
看起来Process
接受*args
和**kwargs
,这意味着当您递交r"C:\Users\Nick\Documents\production\TEST\\test.txt"
时,它会迭代它以提供*args == ["C",":","\\","U","s","e","r","s","\\","N", ... ]