与flock()函数有关的问题

发布于 2024-09-27 12:11:19 字数 353 浏览 3 评论 0原文

我有一个关于flock()如何工作的问题,特别是在Python中。我有一个打开串行连接的模块(通过 os.open() )。我需要确保这个线程的安全。使用 threading.Lock() 在同一个模块中工作时,很容易使其线程安全,但如果模块从不同的位置导入,它就会崩溃。

我正在考虑使用flock(),但我很难找到足够的关于flock到底如何工作的信息。我读到,一旦文件关闭,flock()就会解锁文件。但是,如果 python 崩溃,是否存在使文件保持打开状态的情况?

如果设置了 LOCK_EX,究竟允许什么使用锁定的文件?只是锁定文件的模块?从最初运行的脚本导入的任何模块?

I have a question about how flock() works, particularly in python. I have a module that opens a serial connection (via os.open()). I need to make this thread safe. It's easy enough making it thread safe when working in the same module using threading.Lock(), but if the module gets imported from different places, it breaks.

I was thinking of using flock(), but I'm having trouble finding enough information about how exactly flock works. I read that flock() unlocks the file once the file is closed. But is there a situation that will keep the file open if python crashes?

And what exactly is allowed to use the locked file if LOCK_EX is set? Just the module that locked the file? Any module that was imported from the script that was originally run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-10-04 12:11:19

当进程终止时,操作系统应该清理所有打开的文件资源(我确信有一些警告)。这是因为关闭文件时会释放咨询锁,该操作是 python 进程退出时操作系统清理的一部分。

请记住, flock(2) 只是建议性的:

咨询锁允许协作进程对文件执行一致的操作,但[其他行为不良的]进程仍可能在不使用咨询锁的情况下访问这些文件。

flock(2) 实现了读写器锁。您无法使用 LOCK_EX 对同一个文件进行两次集群,但任意数量的人都可以同时使用 LOCK_SH 对同一文件进行集群(只要没有其他人拥有 LOCK_EX< /code> 就可以了)。

锁定机制允许两种类型的锁:共享锁和排它锁。任何时候都可以对一个文件应用多个共享锁,但任何时候都不允许在一个文件上同时使用多个独占锁或共享锁和独占锁。

集群工作在操作系统/进程级别,并且独立于Python模块。一个模块可能请求 n 个锁,或者跨 m 个模块请求 n 个锁。但是,在给定时间只有一个进程可以持有给定文件的 LOCK_EX 锁。

“非 UNIX”系统或非本地文件系统上的 YMMV。

When a process dies the OS should clean up any open file resources (with some caveats, I'm sure). This is because the advisory lock is released when the file is closed, an operation which occurs as part of the OS cleanup when the python process exits.

Remember, flock(2) is merely advisory:

Advisory locks allow cooperating processes to perform consistent operations on files, but [other, poorly behaved] processes may still access those files without using advisory locks.

flock(2) implements a readers-writer lock. You can't flock the same file twice with LOCK_EX, but any number of people can flock it with LOCK_SH simultaneously (as long as nobody else has a LOCK_EX on it).

The locking mechanism allows two types of locks: shared locks and exclusive locks. At any time multiple shared locks may be applied to a file, but at no time are multiple exclusive, or both shared and exclusive, locks allowed simultaneously on a file.

flock works at the OS/process level and is independent of python modules. One module may request n locks, or n locks could be requested across m modules. However, only one process can hold a LOCK_EX lock on a given file at a given time.

YMMV on a "non-UNIX" system or a non-local filesystem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文