With Python, to simulate a genuine parallel-processing (making use of all the cores) #3
-
With Python, to simulate a genuine parallel-processing (making use of all the cores), who could assist me with a working example code? which I couldn't manage to find out, Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
We have a couple of options for parallel processing in Python:
An example of the reliable way to utilize all cores for CPU-bound tasks: def cpu_intensive_task(n): if name == 'main':
` FYI, CPU-bound vs I/O-bound tasks: Global Interpreter Lock (GIL): I can share with you a few useful links and resources if you're further interested in. |
Beta Was this translation helpful? Give feedback.
We have a couple of options for parallel processing in Python:
An example of the reliable way to utilize all cores for CPU-bound tasks:
`
import multiprocessing
import time
def cpu_intensive_task(n):
"""Example CPU-bound task (calculates nth Fibonacci number)"""
if n <= 1:
return n
else:
return cpu_intensive_task(n-1) + cpu_intensive_task(n-2)
if name == 'main':
# Get number of available CPU cores
num_cores = multiprocessing.cpu_count()
print(f"Using {num_cores} CPU …