Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 117 characters in body
Source Link
Andrew Wei
  • 900
  • 1
  • 7
  • 13

You can do this using multiprocessing.Queue and multiprocessing.Queue.get. How this works is that get by default blocks until there's something in the queue. So it will return the first result that gets appended to the queue, i.e. one of the processes finishing the search. After that, we can iterate over the processes and terminate each one (note that terminating a process doesn't kill child processes spawned by the process unless daemon is set to True).

import multiprocessing import random import time FIND = 50 MAX_COUNT = 100000 INTERVAL = range(10) queue = multiprocessing.Queue(maxsize=1) def find(process, initial): succ = False while succ == False: start=initial while(start <= MAX_COUNT): if(FIND == start): queue.put(f"Found: {process}, start: {initial}") break; i = random.choice(INTERVAL) start = start + i print(process, start) processes = [] manager = multiprocessing.Manager() for i in range(5): process = multiprocessing.Process(target=find, args=(f'computer_{i}', i)) processes.append(process) process.start() ret = queue.get() for i in range(5): process = processes[i] process.terminate() print(f'terminated {i}') print(ret) 

You might also want to look into setting the daemon, which kills the processes after the main process exits.

You can do this using multiprocessing.Queue and multiprocessing.Queue.get. How this works is that get by default blocks until there's something in the queue. So it will return the first result that gets appended to the queue, i.e. one of the processes finishing the search. After that, we can iterate over the processes and terminate each one.

import multiprocessing import random import time FIND = 50 MAX_COUNT = 100000 INTERVAL = range(10) queue = multiprocessing.Queue(maxsize=1) def find(process, initial): succ = False while succ == False: start=initial while(start <= MAX_COUNT): if(FIND == start): queue.put(f"Found: {process}, start: {initial}") break; i = random.choice(INTERVAL) start = start + i print(process, start) processes = [] manager = multiprocessing.Manager() for i in range(5): process = multiprocessing.Process(target=find, args=(f'computer_{i}', i)) processes.append(process) process.start() ret = queue.get() for i in range(5): process = processes[i] process.terminate() print(f'terminated {i}') print(ret) 

You might also want to look into setting the daemon, which kills the processes after the main process exits.

You can do this using multiprocessing.Queue and multiprocessing.Queue.get. How this works is that get by default blocks until there's something in the queue. So it will return the first result that gets appended to the queue, i.e. one of the processes finishing the search. After that, we can iterate over the processes and terminate each one (note that terminating a process doesn't kill child processes spawned by the process unless daemon is set to True).

import multiprocessing import random import time FIND = 50 MAX_COUNT = 100000 INTERVAL = range(10) queue = multiprocessing.Queue(maxsize=1) def find(process, initial): succ = False while succ == False: start=initial while(start <= MAX_COUNT): if(FIND == start): queue.put(f"Found: {process}, start: {initial}") break; i = random.choice(INTERVAL) start = start + i print(process, start) processes = [] manager = multiprocessing.Manager() for i in range(5): process = multiprocessing.Process(target=find, args=(f'computer_{i}', i)) processes.append(process) process.start() ret = queue.get() for i in range(5): process = processes[i] process.terminate() print(f'terminated {i}') print(ret) 

You might also want to look into setting the daemon, which kills the processes after the main process exits.

Source Link
Andrew Wei
  • 900
  • 1
  • 7
  • 13

You can do this using multiprocessing.Queue and multiprocessing.Queue.get. How this works is that get by default blocks until there's something in the queue. So it will return the first result that gets appended to the queue, i.e. one of the processes finishing the search. After that, we can iterate over the processes and terminate each one.

import multiprocessing import random import time FIND = 50 MAX_COUNT = 100000 INTERVAL = range(10) queue = multiprocessing.Queue(maxsize=1) def find(process, initial): succ = False while succ == False: start=initial while(start <= MAX_COUNT): if(FIND == start): queue.put(f"Found: {process}, start: {initial}") break; i = random.choice(INTERVAL) start = start + i print(process, start) processes = [] manager = multiprocessing.Manager() for i in range(5): process = multiprocessing.Process(target=find, args=(f'computer_{i}', i)) processes.append(process) process.start() ret = queue.get() for i in range(5): process = processes[i] process.terminate() print(f'terminated {i}') print(ret) 

You might also want to look into setting the daemon, which kills the processes after the main process exits.