let' try to parallelize compute-heavy operation using similar constructs to those provided by the threading apifrom multiprocessing import processcpu_count import time import os class muchcpu(process)def run(self)print(os getpid()for in range( )pass if __name__ ='__main__'procs [muchcpu(for in range(cpu_count()) time time(for in procsp start(for in procsp join(print('work took {secondsformat(time time( )this example just ties up the cpu for million iterations you may not consider this to be useful workbut it' cold day and appreciate the heat my laptop generates under such load the api should be familiarwe implement subclass of process (instead of threadand implement run method this method prints out the process id ( unique number the operating system assigns to each process on the machinebefore doing some intense (if misguidedwork pay special attention to the if __name__ ='__main__'guard around the module level code that prevents it to run if the module is being importedrather than run as program this is good practice in generalbut when using multiprocessing on some operating systemsit is essential behind the scenesmultiprocessing may have to import the module inside the new process in order to execute the run(method if we allowed the entire module to execute at that pointit would start creating new processes recursively until the operating system ran out of resources we construct one process for each processor core on our machinethen start and join each of those processes on my era quad-core laptopthe output looks like this