Skip to content

Instantly share code, notes, and snippets.

@mbiemann
Last active November 14, 2021 04:12
Show Gist options
  • Save mbiemann/3952dbd0a4e9a92fd1e918822f595b13 to your computer and use it in GitHub Desktop.
Save mbiemann/3952dbd0a4e9a92fd1e918822f595b13 to your computer and use it in GitHub Desktop.
Game Of Thrones - Election Parallel Sequence Voting
import json, random, time
from multiprocessing import Process
candidates = json.load(open('candidates.json','r'))
total_of_candidates = len(candidates)
# people in sequence X district in parallel = total of voting
people_in_sequence = 3
district_in_parallel = 3
# define a function that voting in sequence and start in parallel
def sequence_of_voting(district):
for i in range(1,people_in_sequence+1):
time.sleep(random.random())
chosen = candidates[random.randint(1,total_of_candidates)-1]
print(f'The chosen candidate {i} in district {district} is "{chosen}".')
# start processes in parallel and wait until all ends
if __name__ == '__main__':
processes = list()
for x in range(1,district_in_parallel+1):
p = Process(target=sequence_of_voting, args=(x,))
processes.append(p)
p.start()
for p in processes:
p.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment