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
Release: 2024-11-21 06:26:10
Original
544 people have browsed it

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.
Copy after login

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)
Copy after login

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()
Copy after login

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)
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template