Skip to content

Instantly share code, notes, and snippets.

@MarkLavrynenko
Last active August 29, 2015 14:23
Show Gist options
  • Save MarkLavrynenko/e411985a9dd0a496f7b3 to your computer and use it in GitHub Desktop.
Save MarkLavrynenko/e411985a9dd0a496f7b3 to your computer and use it in GitHub Desktop.
Discardable distributed job
from multiprocessing import Process, Manager, Event
from random import randint
children_count = 5
block_size = 10000
def find(my_id, found):
print("Process with id %s has been started" % my_id)
left = block_size * my_id
right = left + block_size
while not found.is_set():
k = randint(left, right)
if (k == 20023):
print("Fucking found it!!!!(Process %s)" % my_id)
found.set()
if __name__ == "__main__":
sync_manager = Manager()
found = sync_manager.Event()
processes = []
for i in range(0, children_count):
p = Process(target=find, args=(i, found))
p.start()
processes.append(p)
found.wait()
print("Finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment