I've been spending some time trying to understand multiprocessing, though its finer points evade my untrained mind. I've been able to get a pool to return a simple integer, but if the function doesn't just return a result like all of the examples I can find (even in the documentation, it's some obscure example I can't quite understand.
Here is an example I'm trying to get working. BUT, I can't get it working as intended, and I'm sure there's a simple reason why. I may need to use a queue or shared memory or a manager, but as many times as I read the documentation I can't seem to wrap my brain around what it actually means and what it does. All I've been able to get an understanding of so far is the pool function.
Also, I'm using a class as I need to avoid using global variables as in this question's answer.
import random
class thisClass:
def __init__(self):
self.i = 0
def countSixes(myClassObject):
newNum = random.randrange(0,10)
#print(newNum) #this proves the function is being run if enabled
if newNum == 6:
myClassObject.i += 1
if __name__ == '__main__':
import multiprocessing
pool = multiprocessing.Pool(1) #use one core for now
counter = thisClass()
myList = []
[myList.append(x) for x in range(1000)]
#it must be (args,) instead of just i, apparently
async_results = [pool.apply_async(countSixes, (counter,)) for i in myList]
for x in async_results:
x.get(timeout=1)
print(counter.i)
Can someone explain in dumb-dumb what needs to be done so I can finally understand what I'm missing and what it does?