dabeaz / curio

Good Curio!
Other
4.03k stars 241 forks source link

exlusive write lock for AsyncFile #289

Closed alekibango closed 5 years ago

alekibango commented 5 years ago

I am afraid another (my) curio application will change a file while i am still writing it.

What is the best way to get exclusive lock? (on posix system only) I think this could be usefull to have supported directly in AsyncFile.

thanks

njsmith commented 5 years ago

File locking on posix systems is a huge mess: http://0pointer.de/blog/projects/locking.html

In addition to the issues noted there, there's no async API for it at all. So if you want to use it from curio you have to use threads and polling.

Python does expose the file locking APIs though to do that, in the fcntl module: https://docs.python.org/3/library/fcntl.html

jab commented 5 years ago

Linux caveats notwithstanding, would you recommend using a library like https://fasteners.readthedocs.io?

dabeaz commented 5 years ago

File locking really seems outside the scope of Curio to me. However, if there is a way to do it, you'd probably want to use the Curio run_in_thread() or block_in_thread(). The latter would be better if you had a LOT of Curio tasks all waiting on the same file lock at once.

alekibango commented 5 years ago

Thanks to all for input! I will later try to add example code to curio examples.