Skip to content

Instantly share code, notes, and snippets.

@antoncohen
Created October 24, 2016 00:51
Show Gist options
  • Save antoncohen/c81799f75cf48d761fbd56e013c4bd3c to your computer and use it in GitHub Desktop.
Save antoncohen/c81799f75cf48d761fbd56e013c4bd3c to your computer and use it in GitHub Desktop.
sleep_lots_max.py
from multiprocessing import Pool, cpu_count
from random import randint
from time import sleep
class RandSleep(object):
"""multiprocessing.Pool.imap* needs to pickle the function it send to the
processes. It can't pickle functions or instance methods. But oddly it
can pickle instances of custom classes. So write the function as a
callable class."""
def __call__(self, max_time):
sleep_time = randint(0, max_time)
sleep(sleep_time)
return sleep_time, max_time
class Sleepy(object):
def increasing_sleeps(self):
"""Pointless function to show using your own generator"""
for i in xrange(10):
yield i
def sleep_lots(self):
"""Takes the output of increasing_sleeps() and send it to rand_sleep(),
yielding the result"""
# Use half of available cores, which is all physical cores if using
# Hyper-Threading, or use one core on uniprocessor systems.
cores = max(cpu_count() // 2, 1)
pool = Pool(cores)
rand_sleep = RandSleep()
# imap_unordered returns the results when they are ready,
# regular imap returns them in the order sent
for sleep_time, max_time in pool.imap_unordered(rand_sleep, self.increasing_sleeps()):
yield sleep_time, max_time
sleepy = Sleepy()
for sleep_time, max_time in sleepy.sleep_lots():
print 'Slept', sleep_time, 'seconds of max', max_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment