Created
June 23, 2018 09:58
-
-
Save walkingpendulum/50702ef9ab4e68ef122cff0b84013174 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from itertools import zip_longest | |
import random | |
from multiprocessing import Process | |
def f(inflow_cache): | |
time.sleep(random.randint(2, 5)) | |
inflow_cache = list(filter(lambda t: t is not None, inflow_cache)) | |
print('average sum', sum(inflow_cache, 0.) / len(inflow_cache)) | |
def get_flow(): | |
init_state = [random.randint(1, random.randint(1, 10)) for _ in range(random.randint(1, 10))] | |
for record in init_state: | |
yield record | |
for _ in range(random.randint(10, 20)): | |
yield random.randint(1, random.randint(1, 10)) | |
def grouper(iterable, n, fillvalue=None): | |
args = [iter(iterable)] * n | |
return zip_longest(fillvalue=fillvalue, *args) | |
if __name__ == '__main__': | |
processes = [] | |
for chunk in grouper(get_flow(), 5): | |
p = Process(target=f, args=(chunk,)) | |
p.start() | |
processes.append(p) | |
[p.join() for p in processes] | |
print('That\'s all, folks!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment