Python:锁定文件 [英] Python: Lock a file

查看:77
本文介绍了Python:锁定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Linux上运行的Python应用程序.从cron每分钟调用一次.它会检查目录中是否有文件,如果找到该目录,则会对其进行处理-这可能需要几分钟.我不希望下一个Cron作业提取当前正在处理的文件,所以我使用下面的代码将其锁定,该代码调用Portalocker.问题是它似乎不起作用.下一个cron作业设法获得已准备好处理的文件的文件句柄.

I have a Python app running on Linux. It is called every minute from cron. It checks a directory for files and if it finds one it processes it - this can take several minutes. I don't want the next cron job to pick up the file currently being processed so I lock it using the code below which calls portalocker. The problem is it doesn't seem to work. The next cron job manages to get a file handle returned for the file all ready being processed.

def open_and_lock(full_filename):
    file_handle = open(full_filename, 'r')
    try:
        portalocker.lock(file_handle, portalocker.LOCK_EX
                            | portalocker.LOCK_NB)
        return file_handle
    except IOError:
        sys.exit(-1)

有什么办法可以锁定文件,所以没有其他进程可以获取它吗?

Any ideas what I can do to lock the file so no other process can get it?

更新

由于@Winston Ewert,我检查了一下代码,发现文件句柄在处理完成之前已关闭.除了似乎在portalocker.lock上的第二个处理块而不是引发异常之外,它现在似乎正在工作.

Thanks to @Winston Ewert I checked through the code and found the file handle was being closed way before the processing had finished. It seems to be working now except the second process blocks on portalocker.lock rather than throwing an exception.

推荐答案

您正在使用LOCK_NB标志,这意味着该调用是非阻塞的,并且只会在失败时立即返回.这大概是在第二个过程中发生的.它仍然能够读取文件的原因是Portalocker最终使用了flock(2)锁,并且如 flock(2)手册页:

You're using the LOCK_NB flag which means that the call is non-blocking and will just return immediately on failure. That is presumably happening in the second process. The reason why it is still able to read the file is that portalocker ultimately uses flock(2) locks, and, as mentioned in the flock(2) man page:

flock(2)仅放置咨询锁; 授予文件适当的权限, 一个过程可以随意忽略使用 flock(2)并对该文件执行I/O.

flock(2) places advisory locks only; given suitable permissions on a file, a process is free to ignore the use of flock(2) and perform I/O on the file.

要解决此问题,您可以直接使用 fcntl.flock 函数(portalocker只是在Linux上的薄包装),然后检查返回的值以查看锁定是否成功.

To fix it you could use the fcntl.flock function directly (portalocker is just a thin wrapper around it on Linux) and check the returned value to see if the lock succeeded.

这篇关于Python:锁定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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