I use the pathos
ProcessingPool
class to schedule concurrent execution of the run_regex()
function across multiple cores. The function takes a regular expression as an argument and evaluates list entries for a match. If a match is found, it puts the matching value into result_queue
.
As I understand, currently each worker process creates a local copy of result_queue
in its virtual address space. However, I'd like to use this Queue object as a shared memory mechanism in order to access all matches from the main process.
Questions:
- Is there a way to pass a Queue object into the Pool initializer, so the queue acts as a shared memory section?
- Is synchronization required with Queue objects?
- Is there a better way to approach this problem?
Code Snippet
from multiprocessing import Lock, Queue
from pathos.multiprocessing import ProcessingPool
result_queue = Queue()
lock = Lock()
data = {}
def run_regex(self, expr):
for key, value in data.iteritems():
matchStr = re.search(expr, key, re.I)
if matchStr:
lock.acquire()
result_queue.put(key)
lock.release()
break
def check_path(self):
pool = ProcessingPool()
pool.map(run_regex, in_regex)