I'm using ThreadPool
from multiprocessing.pool
but it seems it is not working (i.e. it is executed sequentially rather than in parallel: res1
takes 14s, res2
takes 11s, and using ThreadPool
takes 27s instead of 14s).
I think the problem is in another_method
because it uses a (shared) read_only_resource
.
I've tried having a time.sleep(val)
in another_method
instead of calling another method (works as expected - it takes as long as the maximum value I pass) and I've also tried to pass a deep copy of the read_only_resource
(this doesn't work, it takes 27s).
I have run out of things to try to make this work:
def method(text_type, read_only_resource):
value = some_processesing(text_type)
return another_method(value, read_only_resource)
def main():
same_read_only_resource = get_read_only_resource()
pool = ThreadPool(processes=2)
res1 = pool.apply_async(method, (some_text_type, same_read_only_resource))
res2 = pool.apply_async(method, (other_text_type, same_read_only_resource))
results1 = res1.get()
results2 = res2.get()