I tried to use multithread pool as in this question. But I want pack all logic to my own class as below. The problem occures in apply_async
callback function. When I pack all logic in class, callback function seems to never be called. I do not know how to assign callback functions so that it will be called correctly. In source question there is only result
in log_result
parameters but I must add additional self
parameters.
import numpy
import pandas as pd
import multiprocessing as mp
from multiprocessing import freeze_support
class MutliThread() :
def __init__(self):
self.result_list = []
def foo_pool(index, number):
data = []
notFound = []
try :
data.append(index + number)
except Exception:
notFound.append(index + number)
return data
def log_result(self, result):
# This is called whenever foo_pool(i) returns a result.
# result_list is modified only by the main process, not the pool workers.
self.result_list.append(self, result)
def apply_async_with_callback(self):
pool = mp.Pool()
data = [1,2,3,4,5,6]
for index, tarrif in enumerate(data) :
pool.apply_async(self.foo_pool, args = (index, tarrif), callback = self.log_result)
pool.close()
pool.join()
print(self.result_list)
if __name__ == '__main__':
freeze_support()
multiThread = MutliThread()
multiThread.apply_async_with_callback()