Junior — Middle
How can multithreaded processing of CPU-bound tasks be performed in Python?
sobes.tech AI
Answer from AI
In Python, for multithreaded processing of CPU-bound tasks, the standard threading module is inefficient due to the GIL (Global Interpreter Lock), which prevents multiple threads from executing Python bytecode simultaneously.
To effectively utilize multiple CPU cores, it is better to use the multiprocessing module, which creates separate processes with their own Python interpreter, bypassing the GIL limitation.
Example of using multiprocessing:
from multiprocessing import Pool
def cpu_bound_task(x):
# example computational task
return x * x
if __name__ == '__main__':
with Pool(processes=4) as pool:
results = pool.map(cpu_bound_task, range(10))
print(results)
This approach allows parallel execution of CPU-intensive tasks and significantly speeds up their execution.