从函数内启动并行进程



我想通过调用一个函数来启动多个进程:

我想在函数中放入以下内容,并且只需要调用该函数:

def func():
print("hey")
if __name__ == '__main__':
processes = []
processes.append(Process(target=func))
processes.append(Process(target=func))
for process in processes:
process.start()

代码的样子:

from multiprocessing import Process, Manager

def multi():
def func():
print("hey")
if __name__ == '__main__':
processes = []
processes.append(Process(target=func))
processes.append(Process(target=func))
for process in processes:
process.start()
#Run function
multi()

我得到的错误:

new_handle = reduction.steal_handle(parent_pid, pipe_handle)
File *path*, line 82, in steal_handle
_winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] The parameter is incorrect

主函数不需要在多函数内部完成。此外,如果您希望函数是多处理的,请将其置于multi之外。您可以将代码重写为:

from multiprocessing import Process
def func():
print("hey")
def multi():
procs=[]
no_of_processes=2 #say
for i in range(no_of_processes):
Process(target=func).start()
for proc in procs:
proc.join()
if __name__=='__main__':
multi()

相关内容

  • 没有找到相关文章

最新更新