4
I’m studying the module multiprocessing of Python, in which allow me to run several processes in parallel (in my understanding). As an illustration case let’s see the example below:
import time
from multiprocessing import Process
def create_process(func):
return Process(target=func)
def run_process(process_list):
for process in process_list:
process.start()
for process in process_list:
process.join()
def task1():
for i in range(5):
print('Task 1', i + 1)
time.sleep(1)
def task2():
for i in range(5):
print('Task 2', i + 1)
time.sleep(1)
run_process([create_process(task1), create_process(task2)])
In which generates the following output:
Task 2 1
Task 1 1
Task 2 2
Task 1 2
Task 1 3
Task 2 3
Task 1 4
Task 2 4
Task 1 5
Task 2 5
Note that for our illustration example I created 2 processes, and it is at this point that starts to arise my doubts that are connected with the amount of processor cores and the ability to perform the task in parallel.
Doubts
- Each process runs on a CPU core so independent/parallel?
- When I go to create several parallel processes I should consider the number of CPU cores? Example: 8 CPU cores I must create only eight processes?