Skip to content

Instantly share code, notes, and snippets.

@byrney
Created December 7, 2016 06:57
Show Gist options
  • Save byrney/9bd5a52aca583ec86b096e06692bdd94 to your computer and use it in GitHub Desktop.
Save byrney/9bd5a52aca583ec86b096e06692bdd94 to your computer and use it in GitHub Desktop.
Share state between python coprocesses
import time
import random
from multiprocessing import Process, Queue, current_process, freeze_support
#
# Function run by worker processes
#
def worker(state, input, output):
key_len = len(state)
for arg in iter(input.get, 'STOP'):
result = "Process: {} lookup: {} -> {}".format(current_process().name,
arg, state[arg % key_len])
time.sleep(2)
output.put(result)
def test():
NUMBER_OF_PROCESSES = 4
TASKS1 = [i for i in range(20)]
TASKS2 = [i + 1 for i in range(10)]
# Create queues
task_queue = Queue()
done_queue = Queue()
# Submit tasks
for task in TASKS1:
task_queue.put(task)
state = {0: 0, 1: 10, 2: 20, 3: 30, 4: 40}
# Start worker processes
for i in range(NUMBER_OF_PROCESSES):
Process(target=worker, args=(state, task_queue, done_queue)).start()
# Get and print results
print('Unordered results:')
for i in range(len(TASKS1)):
print('\t', done_queue.get())
# Add more tasks using `put()`
for task in TASKS2:
task_queue.put(task)
# Get and print some more results
for i in range(len(TASKS2)):
print('\t', done_queue.get())
# Tell child processes to stop
for i in range(NUMBER_OF_PROCESSES):
task_queue.put('STOP')
if __name__ == '__main__':
freeze_support()
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment