문제

I have this code:

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()

I am just testing how to use multiprocessing at the moment, but the get_md5 function throws a type error. the error message is this:

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)

It looks to me as if there is only one argument for get_md5 process, I have no idea where 48 arguments are coming from.

Can anyone help?

도움이 되었습니까?

해결책

I think, you have to pass args as tuple:

s = mp.Process(target=get_md5, args=(file_1,))

You are missing the comma. If you miss the comma, individual characters in file_1 treated as separate args.

EDIT:

Including Adam Smith's response which i think appropriate to context:

It looks like Process accepts *args and **kwargs, which means when you hand it r"C:\Users\Nick\Documents\production\TEST\\test.txt" it iterates over it to give *args == ["C",":","\\","U","s","e","r","s","\\","N", ... ]

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top