Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement