👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I am trying my very first formal python program using Threading and Multiprocessing on a windows machine. I am unable to launch the processes though, with python giving the following message. The thing is, I am not launching my threads in the main module. The threads are handled in a separate module inside a class.
EDIT: By the way this code runs fine on ubuntu. Not quite on windows
RuntimeError:
Attempt to start a new process before the current process
has finished its bootstrapping phase.
This probably means that you are on Windows and you have
forgotten to use the proper idiom in the main module:
if __name__ == "__main__":
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce a Windows executable.
My original code is pretty long, but I was able to reproduce the error in an abridged version of the code. It is split in two files, the first is the main module and does very little other than import the module which handles processes/threads and calls a method. The second module is where the meat of the code is.
testMain.py:
import parallelTestModule
extractor = parallelTestModule.ParallelExtractor()
extractor.runInParallel(numProcesses=2, numThreads=4)
parallelTestModule.py:
import multiprocessing
from multiprocessing import Process
import threading
class ThreadRunner(threading.Thread):
""" This class represents a single instance of a running thread"""
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
print self.name,"
"
class ProcessRunner:
""" This class represents a single instance of a running process """
def runp(self, pid, numThreads):
mythreads = []
for tid in range(numThreads):
name = "Proc-"+str(pid)+"-Thread-"+str(tid)
th = ThreadRunner(name)
mythreads.append(th)
for i in mythreads:
i.start()
for i in mythreads:
i.join()
class ParallelExtractor:
def runInParallel(self, numProcesses, numThreads):
myprocs = []
prunner = ProcessRunner()
for pid in range(numProcesses):
pr = Process(target=prunner.runp, args=(pid, numThreads))
myprocs.append(pr)
# if __name__ == "parallelTestModule": #This didnt work
# if __name__ == "__main__": #This obviously doesnt work
# multiprocessing.freeze_support() #added after seeing error to no avail
for i in myprocs:
i.start()
for i in myprocs:
i.join()
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from RuntimeError on windows trying python multiprocessing, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano RuntimeError on windows trying python multiprocessing
- Deutsch RuntimeError on windows trying python multiprocessing
- Français RuntimeError on windows trying python multiprocessing
- Español RuntimeError on windows trying python multiprocessing
- Türk RuntimeError on windows trying python multiprocessing
- Русский RuntimeError on windows trying python multiprocessing
- Português RuntimeError on windows trying python multiprocessing
- Polski RuntimeError on windows trying python multiprocessing
- Nederlandse RuntimeError on windows trying python multiprocessing
- 中文 RuntimeError on windows trying python multiprocessing
- 한국어 RuntimeError on windows trying python multiprocessing
- 日本語 RuntimeError on windows trying python multiprocessing
- हिन्दी RuntimeError on windows trying python multiprocessing
Prague | 2023-03-21
Maybe there are another answers? What RuntimeError on windows trying python multiprocessing exactly means?. Checked yesterday, it works!
Munchen | 2023-03-21
I was preparing for my coding interview, thanks for clarifying this - RuntimeError on windows trying python multiprocessing in Python is not the simplest one. Will use it in my bachelor thesis
Warsaw | 2023-03-21
Thanks for explaining! I was stuck with RuntimeError on windows trying python multiprocessing for some hours, finally got it done 🤗. Will use it in my bachelor thesis