Skip to content

Instantly share code, notes, and snippets.

@Andrei-Pozolotin
Created December 6, 2019 18:05
Show Gist options
  • Save Andrei-Pozolotin/3e548c640000982de98f0b701c7e0b21 to your computer and use it in GitHub Desktop.
Save Andrei-Pozolotin/3e548c640000982de98f0b701c7e0b21 to your computer and use it in GitHub Desktop.
master nice=0
worker index=2 nice=19
worker index=1 nice=19
worker index=3 nice=19
worker index=4 nice=19
worker index=5 nice=19
worker index=6 nice=19
worker index=7 nice=19
worker index=9 nice=19
worker index=8 nice=19
...
ratio=[9, 8, 9, 9, 7, 10, 7, 9, 16, 11]
ratio=[9, 8, 9, 9, 7, 10, 7, 8, 16, 11]
ratio=[9, 8, 9, 9, 7, 10, 7, 8, 16, 11]
ratio=[9, 8, 9, 9, 7, 10, 7, 8, 16, 11]
ratio=[9, 7, 9, 9, 7, 10, 7, 8, 16, 11]
#!/usr/bin/env python
import os
import time
import ctypes
import multiprocessing as mp
process_count = 10
count_list = mp.RawArray(ctypes.c_long, process_count)
for index in range(process_count) :
count_list[index] = 0
def master_code(count_list:mp.Array) -> None:
""
nice = os.nice(+0)
print(f"master nice={nice}")
while True:
count_list[0] += 1
def worker_code(index:int, count_list:mp.Array) -> None:
""
nice = os.nice(+100)
print(f"worker index={index} nice={nice}")
while True:
count_list[index] += 1
master_proc = mp.Process(target=master_code, args=(count_list,))
master_proc.name = "master"
master_proc.start()
for index in range(1, process_count):
worker_proc = mp.Process(target=worker_code, args=(index, count_list,))
worker_proc.name = f"worker-{index}"
worker_proc.start()
while True:
time.sleep(1)
total = sum(count_list)
ratio = [int(100 * entry / total) for entry in count_list]
print(f"ratio={ratio}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment