跨进程级锁可以使独立进程间互斥地访问某共享资源(比如,多进程共同写一个文件)。
这里通过文件锁实现,源码如下:
# coding: utf-8 # processlock.py # 进程锁 - 进程间互斥锁 import os try: import fcntl LOCK_EX = fcntl.LOCK_EX except ImportError: # Windows平台下没有fcntl模块 fcntl = None import win32con import win32file import pywintypes LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK overlapped = pywintypes.OVERLAPPED() class Lock: """进程锁 """ def __init__(self, filename='processlock.txt'): self.filename = filename # 如果文件不存在则创建 self.handle = open(filename, 'w') def acquire(self): # 给文件上锁 if fcntl: fcntl.flock(self.handle, LOCK_EX) else: hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.LockFileEx(hfile, LOCK_EX, 0, -0x10000, overlapped) def release(self): # 文件解锁 if fcntl: fcntl.flock(self.handle, fcntl.LOCK_UN) else: hfile = win32file._get_osfhandle(self.handle.fileno()) win32file.UnlockFileEx(hfile, 0, -0x10000, overlapped) def __del__(self): try: self.handle.close() os.remove(self.filename) except: pass if __name__ == '__main__': # 测试:依次运行本程序多个实例,第N个实例运行耗时是第一个的N倍 import time print 'Time: %s' % time.time() lock = Lock() try: lock.acquire() time.sleep(20) finally: lock.release() print 'Time: %s' % time.time()
参考:
http://blog.vmfarms.com/2011/03/cross-process-locking-and.html
http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/
呵呵,谢谢
VaTG790i.最好的<a href=http://www.kyfei.com>网站推广软件</a>,
非常好
....................
;ui;普i;uighur;ui;ui;个
在unix网络编程中看到了关于TCP/IP的一些内容,我感觉还是写的不够。正在下载中,一定
下载地址呢