Skip to content

Instantly share code, notes, and snippets.

@jupiterbjy
Created May 15, 2024 06:12
Show Gist options
  • Save jupiterbjy/ff8696fe94da24531fc9f516110f6486 to your computer and use it in GitHub Desktop.
Save jupiterbjy/ff8696fe94da24531fc9f516110f6486 to your computer and use it in GitHub Desktop.
Some random code to demonstrate mp
# won't run on notebook
import multiprocessing as mp
import time
from os import getpid
from typing import Tuple
MAX_CPU = 6
def processing_func(task: Tuple[int, int]) -> int:
"""Calculate digits of input**10000000"""
task_id, task_input = task
print(f"[PID {getpid():6}] Processing task {task_id}")
start = time.time()
digits = 0
num = (task_input ** 1000000)
while num:
digits += 1
num //= 10
duration = time.time() - start
print(f"[PID {getpid():6}] Task {task_id} done in {duration:.2f}")
return digits
def main():
tasks = enumerate(range(10))
with mp.Pool(MAX_CPU) as pool:
print("Pool started")
results = pool.map(processing_func, tasks)
print("Pool done, Results:")
for result in results:
print(result)
if __name__ == '__main__':
main()
@jupiterbjy
Copy link
Author

Pool started
[PID  20068] Processing task 0
[PID  20068] Task 0 done in 0.00
[PID  20068] Processing task 1
[PID  20068] Task 1 done in 0.00
[PID  20068] Processing task 2
[PID  20280] Processing task 3
[PID  12944] Processing task 4
[PID  18724] Processing task 5
[PID  18872] Processing task 6
[PID  13120] Processing task 7
[PID  20068] Task 2 done in 16.23
[PID  20068] Processing task 8
[PID  20280] Task 3 done in 40.81
[PID  20280] Processing task 9
[PID  12944] Task 4 done in 65.35
[PID  18724] Task 5 done in 87.44
[PID  18872] Task 6 done in 108.09
[PID  13120] Task 7 done in 128.07
[PID  20068] Task 8 done in 145.13
[PID  20280] Task 9 done in 160.62
Pool done, Results:
0
1
301030
477122
602060
698971
778152
845099
903090
954243

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment