Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- from multiprocessing import Process, Queue
- from Queue import Empty
- import numpy as np
- START = 'START'
- FINAL = 'FINAL'
- STEPS = 'STEPS'
- SIZE = 'SIZE'
- P_IMMUNE = 'P_IMMUNE'
- def run_sim (in_, out_):
- try:
- while True:
- sim = in_.get(False) # get simulation from input
- start = np.zeros((sim[SIZE], sim[SIZE]))
- final = np.array(start) # Clone start
- steps = 0
- # Perform the rest of the simulation
- # Store the results in the dict
- sim[START] = start
- sim[FINAL] = final
- sim[STEPS] = steps
- out_.put(sim) # put result in output
- except Empty:
- pass # I expect to get this when the queue is empty. That's OK.
- cores = 2 # NB. Make sure this is not higher than the number of cores on your
- # computer!
- runs = 10
- n = 10
- p = .5683645 # Number pulled out of my head at quasi-random
- inbox = Queue()
- outbox = Queue()
- for i in xrange(runs):
- inbox.put({
- SIZE: n,
- P_IMMUNE: p
- })
- workers = []
- for i in xrange(cores):
- worker = Process(target=run_sim, args=(inbox,outbox))
- worker.start()
- workers.append(worker)
- results = []
- for i in xrange(runs):
- results.append(outbox.get()) # get results from the workers
- # at this point all results collected workers are stopped.
- for worker in workers:
- worker.join() # 'rightsize' workers
- print results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement