MikA
MikA

Reputation: 5582

Shared memory parallel processing in python

I have a list of strings and I want to bucket these list elements into appropriate buckets.

def bucketElements(i):
    global buckA, buckB, buckC
    if i.startswith('A'):
        buckA.add(i)
    elif i.startswith('B'):
        buckB.add(i)
    elif i.startswith('C'):
        buckC.add(i)

Now I want to call this method in parallel for each element inside my List. Something like this,

buckA,buckB, buckC = set(), set(), set()
pool = multiprocessing.Pool(processes=10)
pool.map(bucketElements, buckList)

Since I'm updating global variables inside my function I cannot use multiprocessing. is there anyway I can improve my processing? currently I'm calling it like this,

buckA,buckB, buckC = set(), set(), set()
for i in buckList:
    bucketElements(i)

Upvotes: 1

Views: 237

Answers (1)

stovfl
stovfl

Reputation: 15533

You have 3 Options:

  • Queue()
  • Manager.Value()
  • Manager.list()

Python » Documentation: multiprocessing.html#managers

Upvotes: 1

Related Questions