Skip to content

Instantly share code, notes, and snippets.

@Niccolum
Last active October 7, 2020 15:25
Show Gist options
  • Save Niccolum/127f7035b09bb68791c92203ed189229 to your computer and use it in GitHub Desktop.
Save Niccolum/127f7035b09bb68791c92203ed189229 to your computer and use it in GitHub Desktop.
from concurrent.futures import ProcessPoolExecutor
import multiprocessing as mp
import time
import random
PROCESSES = 3
def main_proc(queues):
while True:
queue = random.choice(queues)
queue.put('hello pidor')
time.sleep(0.1)
def other_proc(proc_num, queue):
while True:
res = queue.get()
if not res:
time.sleep(1)
continue
print(f'process {proc_num} says: {res}')
def main():
manager = mp.Manager()
queues = [manager.Queue() for _ in range(PROCESSES)]
p = mp.Process(target=main_proc, args=[queues])
p.start()
with ProcessPoolExecutor(max_workers=PROCESSES) as executor:
executor.map(other_proc, range(1, PROCESSES+1), queues)
if __name__ == '__main__':
main()
$ python3 multiproc_tasking.py
process 2 says: hello pidor
process 1 says: hello pidor
process 2 says: hello pidor
process 1 says: hello pidor
process 3 says: hello pidor
process 1 says: hello pidor
process 3 says: hello pidor
process 2 says: hello pidor
process 3 says: hello pidor
process 1 says: hello pidor
process 3 says: hello pidor
process 1 says: hello pidor
process 2 says: hello pidor
process 3 says: hello pidor
process 1 says: hello pidor
process 3 says: hello pidor
process 2 says: hello pidor
process 2 says: hello pidor
process 1 says: hello pidor
process 2 says: hello pidor
process 1 says: hello pidor
process 2 says: hello pidor
process 2 says: hello pidor
process 1 says: hello pidor
process 1 says: hello pidor
process 1 says: hello pidor
process 3 says: hello pidor
process 1 says: hello pidor
process 3 says: hello pidor
process 1 says: hello pidor
process 1 says: hello pidor
process 2 says: hello pidor
process 1 says: hello pidor
process 2 says: hello pidor
process 3 says: hello pidor
process 2 says: hello pidor
process 3 says: hello pidor
process 3 says: hello pidor
process 3 says: hello pidor
process 2 says: hello pidor
process 1 says: hello pidor
process 1 says: hello pidor
process 2 says: hello pidor
process 1 says: hello pidor
process 1 says: hello pidor
process 3 says: hello pidor
process 1 says: hello pidor
process 1 says: hello pidor
process 3 says: hello pidor
process 1 says: hello pidor
process 2 says: hello pidor
process 3 says: hello pidor
process 3 says: hello pidor
process 3 says: hello pidor
process 3 says: hello pidor
process 3 says: hello pidor
process 3 says: hello pidor
process 2 says: hello pidor
process 2 says: hello pidor
process 3 says: hello pidor
process 1 says: hello pidor
process 2 says: hello pidor
process 2 says: hello pidor
process 3 says: hello pidor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment