我正在创建一个看门狗来监视不同虚拟机上的多个文件夹的新文件,正在使用多处理,以便任务同时运行。我只编写了一个所有进程都将使用的函数,但文件夹路径不同。 我正在使用两个变量并提供了它们,但我仍然收到一个错误说
{
"message": "monitor() missing 1 required positional argument: 'saved'"
}
import os
import multiprocessing as mp
def monitor(origin,saved):
new = os.listdir(origin)
if new:
print(new)
for filename in os.listdir(origin):
src = origin+'\'+filename
dst = saved)+'\'+filename
shutil.move(src,dst)
if __name__ == '__main__':
orig1 = 'pth_to_folder'
orig2 = 'pth_to_folder'
orig3 = 'path_to_folder'
# creating processes
p1 = mp.Process(target=monitor, args =(orig1, saved1,))
p2 = mp.Process(target=monitor, args =(orig2, saved2,))
p3 = mp.Process(target=monitor, args =(orig3, saved3,))
# starting processes
p1.start()
p2.start()
p3.start()
# wait until processes are finished
p1.join()
p2.join()
p3.join()
下面是错误日志
{
"message": "monitor() missing 1 required positional argument: 'saved'"
}
将特殊变量(名称(行更改为以下内容,
if __name__ == '__main__':
我尝试了下面这样的东西,它对我有用,
if __name__ == '__main__':
jobs = []
# creating processes
for i in range(3):
p = mp.Process(target=monitor, args=('orig'+str(i+1), 'saved'+str(i+1,)))
jobs.append(p)
p.start()
for job in jobs:
job.join()