如何锁定文件(最简单/最好的方式)? [英] How to lock files (the easiest/best way)?

查看:61
本文介绍了如何锁定文件(最简单/最好的方式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有比使用fnctl更好的东西?看起来有点令人生畏

快速浏览。

Is there something better than using fnctl? It seems a bit intimidating
with a quick look.

推荐答案

2006年7月15日星期六23:28:21 + 0200,Sybren Stuvel写道:
On Sat, 15 Jul 2006 23:28:21 +0200, Sybren Stuvel wrote:

Elmo M?¤ntynen启发我们:
Elmo M?¤ntynen enlightened us with:

>有没有比使用fnctl更好的东西?这看起来有点吓人。
>Is there something better than using fnctl? It seems a bit
intimidating with a quick look.



锁定文件是一项复杂的业务。你想锁定什么?为什么?

锁定它是什么?与文件也必须被锁定,比如通过网络使用

相比,锁定文件以便本地

使用更容易。 />

Sybren


Locking files is a complex business. What do you want to lock? Why?
Lock it with respect to what? It''s easier to lock a file for local
use, compared to when the file also has to be locked from, say, use
via the network.

Sybren



仅限本地。我希望能够从多个可能并行的进程中读取/写入来自

的单个文件。 触摸锁定(或类似的东西

)可靠地工作(这只是发生在我身上)?

Only locally. I want to be able to read/write to a single file from
multiple possibly parallel processes. Would ''touch lock'' (or something
like that) work reliably (this just occured to me)?


星期六,15 2006年7月23:52:10 +0200,Sybren Stuvel写道:
On Sat, 15 Jul 2006 23:52:10 +0200, Sybren Stuvel wrote:

Elmo M?¤ntynen启发我们:
Elmo M?¤ntynen enlightened us with:

>仅限本地。我希望能够从多个可能并行的进程读取/写入单个文件。 触摸锁定(或类似的东西)能否可靠地工作(这只是发生在我身上)?
>Only locally. I want to be able to read/write to a single file from
multiple possibly parallel processes. Would ''touch lock'' (or
something like that) work reliably (this just occured to me)?



我使用锁定目录,os.mkdir(''/ var / lock / somedir'')。

如果你使用一个文件,你需要两个步骤:

1)检查锁文件是否存在

2)创建锁文件


这不是原子的。使用目录,如果它已经存在,则创建它将失败。这意味着您可以自动检查锁定,并且如果它已经不存在,则为
,您也立即创建它。


Sybren


I use a lock directory for that, os.mkdir(''/var/lock/somedir'').
If you use a file, you need two steps:
1) Check whether the lock-file exists
2) Create the lock-file

This is not atomic. With a directory, creating it will fail if it
already exists. This means you can atomically check for the lock, and
if it doesn''t exist already, you''ve immediately created it too.

Sybren



谢谢。这是什么原子基本上意味着什么?

Thanks. Is that what atomic basically means?


在2006-07-15 18:52:10,Sybren Stuvel写道:
On 2006-07-15 18:52:10, Sybren Stuvel wrote:

Elmo M?ntynen启发我们:
Elmo M?ntynen enlightened us with:

>仅限本地。我希望能够从多个可能并行的进程读取/写入单个文件。 触摸锁定(或类似的东西)能否可靠地工作(这只是发生在我身上)?
>Only locally. I want to be able to read/write to a single file from
multiple possibly parallel processes. Would ''touch lock'' (or
something like that) work reliably (this just occured to me)?



我使用锁定目录,os.mkdir(''/ var / lock / somedir'')。

如果你使用文件,你需要两个步骤:

1)检查锁文件是否存在

2)创建锁文件


I use a lock directory for that, os.mkdir(''/var/lock/somedir'').
If you use a file, you need two steps:
1) Check whether the lock-file exists
2) Create the lock-file



cvsnt例如用于使用锁文件。现在它使用一个锁服务器:一个

服务器应用程序,只是坐在那里,允许不同的进程获取,

检查并释放文件锁。更多的实施工作,可能是,

但效率更高。


格哈德

cvsnt for example used to use lock files. Now it uses a lock server: a
server app that just sits there and allows different processes to acquire,
check for and release locks on files. More implementation work, probably,
but more efficient.

Gerhard


这篇关于如何锁定文件(最简单/最好的方式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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