Skip to content

Instantly share code, notes, and snippets.

@karmacoma-eth
Last active May 30, 2023 15:34
Show Gist options
  • Save karmacoma-eth/b5b312d38706be4090a1d506017461ea to your computer and use it in GitHub Desktop.
Save karmacoma-eth/b5b312d38706be4090a1d506017461ea to your computer and use it in GitHub Desktop.
starts n processes with some random amount of work, and they exit whenever the first one is finished
import os
import random
import time
from multiprocessing import Pool, Event
def init_pool_processes(the_shutdown_event):
'''
Initialize each process with the global shutdown event
'''
global shutdown_event
shutdown_event = the_shutdown_event
def wait(ignored):
sleep_time = random.randint(1, 16)
sleep_time_left = sleep_time
print(f"😴 Sleeping for {sleep_time} seconds...")
while True:
if shutdown_event.is_set():
print("πŸ’€ Shutdown event set, exiting...")
return
if sleep_time_left <= 0:
print(f"πŸ˜ƒ Done sleeping for {sleep_time} seconds!")
break
time.sleep(1)
sleep_time_left -= 1
shutdown_event.set()
# starts a pool of n processes with some random amount of work, and they all stop as soon
# as the first one is finished
if __name__ == '__main__':
print(f"πŸ‘·β€β™‚οΈ Starting {os.cpu_count()} worker processes")
shutdown_event = Event()
with Pool(processes=os.cpu_count(), initializer=init_pool_processes, initargs=(shutdown_event,)) as pool:
pool.map(wait, range(os.cpu_count()))
pool.close()
pool.join()
import os
import random
import time
from multiprocessing import Pool, Event
MAX_WAIT = 32
def pretend_uninterruptible_sleep(sleep_time) -> int:
start_time = time.time()
while True:
elapsed_time = time.time() - start_time
if elapsed_time > sleep_time:
return elapsed_time
def wait(cpu_num):
sleep_time = random.randint(16, MAX_WAIT)
print(f"😴 CPU {cpu_num} sleeping for {sleep_time} seconds...")
# pretend that this is black box code where we can't just check for a shutdown event
elapsed_time = pretend_uninterruptible_sleep(sleep_time)
print(f"πŸ˜ƒ Done sleeping for {sleep_time} seconds!")
return (cpu_num, elapsed_time)
# starts a pool of n processes with some random amount of work, and they all stop as soon
# as the first one is finished
if __name__ == '__main__':
print(f"πŸ‘·β€β™‚οΈ Starting {os.cpu_count()} worker processes")
with Pool() as pool:
for (winner_cpu_num, elapsed_time) in pool.imap_unordered(wait, range(os.cpu_count())): # this is a generator
print(f"πŸ† Winner: CPU {winner_cpu_num} in {elapsed_time:.2f}s")
break
print("πŸ”ͺ Brutalizing the pool")
pool.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment