Junior — Middle
Are you familiar with the concept of multiprocessing and its application in programming?
sobes.tech AI
Answer from AI
Multiprocessing is a method of executing multiple processes in parallel within an operating system. In programming, it is used to increase performance, especially when tasks require heavy computation and cannot efficiently utilize multithreading due to limitations such as Python's GIL.
In Python, the multiprocessing module allows creating and managing processes that run in parallel and have their own memory.
An example of simple usage:
from multiprocessing import Process
def worker(num):
print(f'Process {num} is running')
if __name__ == '__main__':
processes = []
for i in range(5):
p = Process(target=worker, args=(i,))
p.start()
processes.append(p)
for p in processes:
p.join()
Here, 5 processes are created that execute the worker function in parallel. This allows efficient utilization of multiple CPU cores.