Home  >  Article  >  Backend Development  >  Why am I getting 'RuntimeError: Attempt to start a new process before the current process has finished its bootstrapping phase' when using multiprocessing on Windows?

Why am I getting 'RuntimeError: Attempt to start a new process before the current process has finished its bootstrapping phase' when using multiprocessing on Windows?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-21 06:26:10545browse

Why am I getting

Windows Multiprocessing Error: RuntimeError

When attempting to utilize multiprocessing on Windows, users may encounter the following error:

RuntimeError:
Attempt to start a new process before the current process
has finished its bootstrapping phase.

This error arises because Windows subprocesses import the main module upon initialization, potentially leading to recursive subprocess creation. To avoid this, it is crucial to include an if __name__ == '__main__': guard in the main module. This prevents the creation of subprocesses from within the main module.

Consider the following code snippet, which demonstrates the issue:

testMain.py:

import parallelTestModule

extractor = parallelTestModule.ParallelExtractor()
extractor.runInParallel(numProcesses=2, numThreads=4)

parallelTestModule.py:

import multiprocessing
from multiprocessing import Process

class ParallelExtractor:
    def runInParallel(self, numProcesses, numThreads):
        myprocs = []
        for pid in range(numProcesses):
            pr = Process(target=self.runp, args=(pid, numThreads))
            myprocs.append(pr)

        for i in myprocs:
            i.start()

        for i in myprocs:
            i.join()

To resolve this error, modify the testMain.py script to include the if __name__ == '__main__': guard:

import parallelTestModule

if __name__ == '__main__':
    extractor = parallelTestModule.ParallelExtractor()
    extractor.runInParallel(numProcesses=2, numThreads=4)

This modification ensures that the subprocesses are not created recursively, resolving the RuntimeError on Windows machines.

The above is the detailed content of Why am I getting 'RuntimeError: Attempt to start a new process before the current process has finished its bootstrapping phase' when using multiprocessing on Windows?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn