Skip to content Skip to sidebar Skip to footer

How Python Manager.dict() Locking Works:

A managers.dict() allow to share a dictionary across process and perform thread-safe operation. In my case each a coordinator process create the shared dict with m elements and n w

Solution 1:

After some tries I can say there is only one lock per managers dict. Here is the code that proves it:

import time
import multiprocessing as mp


defprocess_f(key, shared_dict):
        values = [i for i inrange(64 * 1024 * 1024)]
        print"Writing {}...".format(key)
        a = time.time()
        shared_dict[key] = values
        b = time.time()
        print"released {} in {}ms".format(key, (b-a)*1000)


defmain():
    process_manager = mp.Manager()
    n = 5
    keys = [i for i inrange(n)]
    shared_dict = process_manager.dict({i: i * i for i in keys})

    pool = mp.Pool(processes=n)

    for i inrange(n):
        pool.apply_async(process_f, (keys[i], shared_dict))
    time.sleep(20)


if __name__ == '__main__':
    main()

output:

Writing4...Writing3...Writing1...Writing2...Writing0...released4in3542.7968502msreleased0in4416.22900963msreleased1in6247.48706818msreleased2in7926.97191238msreleased3in9973.71196747msProcessfinishedwithexitcode0

The increasing time for writing show the waiting which is happening.

Post a Comment for "How Python Manager.dict() Locking Works:"