python 模块

fcntl 模块

(只用于 Unix) fcntl 模块为 Unix上的 ioctl 和 fcntl 函数提供了一个接口. 它们用于文件句柄和 I/O 设备句柄的 "out of band" 操作, 包括读取扩展属性, 控制阻塞. 更改终端行为等等. (out of band management: 指使用分离的渠道进行设备管理. 这使系统管理员能在机器关机的时候对服务器, 网络进行监视和管理. 出处:http://en.wikipedia.org/wiki/Out-of-band_management )

关于如何在平台上使用这些函数, 请查阅对应的 Unix man 手册.

该模块同时提供了 Unix 文件锁定机制的接口. Example 展示了如何使用 flock 函数, 更新文件时为文件设置一个advisory lock .

输出结果是由同时运行 3 个副本得到的. 像这样(都在一句命令行里):

python fcntl-example-1.py& python fcntl-example-1.py& python fcntl-example-1.py&

如果你注释掉对 flock 的调用, 那么 counter 文件不会正确地更新.

Example: Using the fcntl Module

File: fcntl-example-1.py

import fcntl, FCNTL
import os, time

FILE = "counter.txt"

if not os.path.exists(FILE):
    # create the counter file if it doesn't exist
        # 创建 counter 文件
    file = open(FILE, "w")
    file.write("0")
    file.close()

for i in range(20):
    # increment the counter
    file = open(FILE, "r+")
    fcntl.flock(file.fileno(), FCNTL.LOCK_EX)
    counter = int(file.readline()) + 1
    file.seek(0)
    file.write(str(counter))
    file.close() # unlocks the file
    print os.getpid(), "=>", counter
    time.sleep(0.1)

*B*30940 => 1
30942 => 2
30941 => 3
30940 => 4
30941 => 5
30942 => 6*b*
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值