python的fcntl.flock函数是否提供文件访问的线程级锁定? [英] Does python's fcntl.flock function provide thread level locking of file access?

查看:294
本文介绍了python的fcntl.flock函数是否提供文件访问的线程级锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的fcnt模块提供了一种称为[flock] [1]的方法来证明文件锁定.描述为:

Python's fcnt module provides a method called [flock][1] to proved file locking. It's description reads:

对文件执行锁定操作 描述符fd(文件对象提供 fileno()方法被接受为 出色地).参见Unix手册flock(2) 有关详细信息. (在某些系统上, 函数使用fcntl()进行仿真.)

Perform the lock operation op on file descriptor fd (file objects providing a fileno() method are accepted as well). See the Unix manual flock(2) for details. (On some systems, this function is emulated using fcntl().)

在linux手册页中查找flock,它仅涉及跨进程锁定,例如:

Looking up the linux man page for flock, it only refers to cross process locking, for example:

如果 不兼容的锁由另一个持有 过程.进行非阻塞 请求,包括LOCK_NB(通过ORing) 以上任何一项操作.

A call to flock() may block if an incompatible lock is held by another process. To make a non-blocking request, include LOCK_NB (by ORing) with any of the above operations.

所以我的问题是:flock()还将提供线程安全锁定并锁定同一进程中的多个线程以及来自不同进程的线程吗?

So my question is: will flock() also provide thread safe locking and lock multiple threads within the same process as well as threads from different processes?

[1]: http://docs.python.org/library/使用fcntl()模拟fcntl.html#fcntl.flockfunction .

推荐答案

flock锁不关心线程-实际上,它们也不关心进程.如果在两个进程中(通过派生继承)使用相同的文件描述符,则使用该FD锁定文件的任何一个进程都将获得两个进程的锁.换句话说,在下面的代码中,两个调用都将返回成功:子进程锁定文件,然后父进程获得相同的锁而不是阻塞,因为它们都是相同的FD.

flock locks don't care about threads--in fact, they don't care about processes, either. If you take the same file descriptor in two processes (inherited through a fork), either process locking the file with that FD will acquire a lock for both processes. In other words, in the following code both flock calls will return success: the child process locks the file, and then the parent process acquires the same lock rather than blocking, because they're both the same FD.

import fcntl, time, os

f = open("testfile", "w+")
print "Locking..."
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
print "locked"
fcntl.flock(f.fileno(), fcntl.LOCK_UN)

if os.fork() == 0:
    # We're in the child process, and we have an inherited copy of the fd.
    # Lock the file.
    print "Child process locking..."
    fcntl.flock(f.fileno(), fcntl.LOCK_EX)
    print "Child process locked..."
    time.sleep(1000)
else:
    # We're in the parent.  Give the child process a moment to lock the file.
    time.sleep(0.5)

    print "Parent process locking..."
    fcntl.flock(f.fileno(), fcntl.LOCK_EX)
    print "Parent process locked"
    time.sleep(1000)

在同一令牌上,如果两次锁定相同的文件,但是使用不同的文件描述符,则这些锁定将相互阻塞,无论您是在同一进程中还是在同一线程中.参见flock(2):If a process uses open(2) (or similar) to obtain more than one descriptor for the same file, these descriptors are treated independently by flock(). An attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor.

On the same token, if you lock the same file twice, but with different file descriptors, the locks will block each other--regardless of whether you're in the same process or the same thread. See flock(2): If a process uses open(2) (or similar) to obtain more than one descriptor for the same file, these descriptors are treated independently by flock(). An attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor.

记住Linux内核中的进程和线程本质上是同一件事,并且内核级API通常将它们视为相同是有用的.在大多数情况下,如果系统调用记录了进程间子/父行为,则对线程也是如此.

It's useful to remember that to the Linux kernel, processes and threads are essentially the same thing, and they're generally treated the same by kernel-level APIs. For the most part, if a syscall documents interprocess child/parent behavior, the same will hold for threads.

当然,您可以(也许应该)自己测试这种行为.

Of course, you can (and probably should) test this behavior yourself.

这篇关于python的fcntl.flock函数是否提供文件访问的线程级锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆