リンク
ちゃんと調べるときは
http://www.python.jp/doc/release/
Wiki内検索
最近更新したページ
最新コメント
文字コードの判定 by awesome things!
正規表現 by awesome things!
辞書 by stunning seo guys
文字列の16進変換 by check it out
文字コードの変換 by check this out
デバッガ by check this out
サブプロセス by watch for this
文字コードの判定 by watch for this
サニタイジング by check this out
タグ

ファイルのロック

プロセス1


>>>
>>> import fcntl
>>>
>>> f = open("/home/tomcat/sqlnet.log", "a+")
>>> fcntl.flock(f.fileno(), fcntl.LOCK_EX)
>>> f.write("書き込みますよ1\n")
>>>

プロセス2


>>>
>>> import fcntl
>>>
>>> f = open("/home/tomcat/sqlnet.log", "a+")
>>> fcntl.flock(f.fileno(), fcntl.LOCK_EX)

ここでブロックされる。

プロセス1 つづき1


>>> fcntl.flock(f.fileno(), fcntl.LOCK_UN)
>>>

プロセス2 つづき1


ブロックが解除されてプロンプトが出ている。

>>>
>>> f.write("書き込みますよa\n")
>>>

プロセス1 つづき2


>>>
>>> fcntl.flock(f.fileno(), fcntl.LOCK_EX)

今度はこっちがブロックされている。

プロセス2 つづき2


>>> fcntl.flock(f.fileno(), fcntl.LOCK_UN)
>>> f.close()
>>>

プロセス1 つづき3


ブロックが解除されてプロンプトが出ている。

>>>
>>> f.write("書き込みますよ2\n")
>>> f.close()
>>>

書き込み結果を見る


$cat /home/tomcat/sqlnet.log
(省略)
書き込みますよa
書き込みますよ1
書き込みますよ2
$

先にクローズしたものが先に書かれている?

偶然かもしれないけど。
2008年07月23日(水) 15:40:55 Modified by hebi_




スマートフォン版で見る