HippoMan
HippoMan

Reputation: 2318

python3: does multiprocessing.Lock() also lock at the thread level?

I have recently switched from threading to multiprocessing in my python3 program. This seems to work fine, and the multiprocessing.Lock() call seems to indeed successfully work as it's supposed to within multiple threads of execution that I spawn from within my program.

However, my program itself is invoked from another, higher-order module which runs separate instances of my program via normal Python threads. I have no ability to rewrite the code of that module.

I previously was using threading.Lock() and threading.RLock() to manage shared code in this environment, and I'm wondering if multiprocessing.Lock() will do the same as threading.Lock() or threading.RLock() when it is invoked within separate threads of the same process.

Or will I have to implement some sort of hybrid locking mechanism to make sure my shared resources are all safe within my combinationmultiprocessing-and-threading environment?

If a hybrid is needed, would the following be sufficient? ...

with multiprocessing.Lock():
    with threading.Lock():  # threading.RLock() in some cases
        # code that I want to protect

... or would I have to implement something more sophisticated, perhaps via a new locking context manager that I would have to write?

Thank you in advance.

Upvotes: 3

Views: 1626

Answers (1)

HippoMan
HippoMan

Reputation: 2318

I finally found the answer to my question: Safe to call multiprocessing from a thread in Python?

I now see that calling multiprocessing.Lock() from within a threaded environment will not work. I had previously posted some code here that created a hybrid multiprocessing/threading lock object, but I have now deleted that code, because it is incorrect.

Upvotes: 3

Related Questions