omnilib / aiomultiprocess

Take a modern Python codebase to the next level of performance.
https://aiomultiprocess.omnilib.dev
MIT License
1.76k stars 99 forks source link

Could we have an async multiprocess-safe lock? #52

Open wdhwg001 opened 4 years ago

wdhwg001 commented 4 years ago

Hi,

Thanks for creating such an amazing lib! I'm asking this because the built-in multiprocessing module has locks which are useful for handling shared memory safely. However, obtaining the lock is a blocking operation which can be slow in some cases.

Therefore, is it possible to have an async version of multiprocessing lock?

Thanks!

amyreese commented 4 years ago

https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Lock

Seems like this could be as simple as a light wrapper around the multiprocessing.Lock class with async acquire/release methods that run the underlying lock methods on a separate thread or executor, with an async context manager alternative as well. I don't have the time to build this myself right now, but would be happy to answer questions or guide others that are interested in contributing this as a PR.

galactic-futon commented 4 years ago

How would one go about specifying the timeout? I'm just learning about async / await but this doesn't SEEM too complex, other than the fact that it looks like a timeout needs to be explicitly calculated based on the context...

amyreese commented 4 years ago

If you want to support timeouts for acquire, then that should likely involve use of asyncio.wait_for, but will also need to make sure to have extra safety checks around the underlying Lock object, so that in case the asyncio wrapper times out, but the underlying acquire succeeded, that the Lock is correctly released at the end. Unit tests covering possible behaviors will likely be critical to guaranteeing correct functionality.

Mihir-Gajera1 commented 1 year ago

@amyreese So is there any way to share the variable between the processes? For Multiprocessing lock and Value/Queue work perfectly. In the case of aio_multiprocess, I am still struggling to share global variables among processes with lock.