| 1 |
import logging |
| 2 |
import multiprocessing |
| 3 |
from time import sleep, perf_counter |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class Integer(object): |
| 16 |
def __init__(self, x): |
| 17 |
self.x = x |
| 18 |
|
| 19 |
|
| 20 |
def largest_prime_factor(n): |
| 21 |
for i in range(10): |
| 22 |
x = [Integer(i * i) for i in range(80000)] |
| 23 |
|
| 24 |
a = x[50] |
| 25 |
print("\033[91mprogress ", n, i, a.x, "\033[0m") |
| 26 |
print("Done") |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
range_obj = range(4) |
| 31 |
if __name__ == "__main__": |
| 32 |
|
| 33 |
|
| 34 |
t0 = perf_counter() |
| 35 |
handles = [ |
| 36 |
multiprocessing.Process(target=largest_prime_factor, args=(i,)) |
| 37 |
for i in range_obj |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
for handle in handles: |
| 42 |
print("Starting", handle) |
| 43 |
handle.start() |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
for handle in handles: |
| 48 |
print("Joining", handle) |
| 49 |
handle.join() |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
dt = perf_counter() - t0 |
| 58 |
print(f"Total time: {dt}") |