[Chicago] help with file locking
Massimo Di Pierro
mdipierro at cs.depaul.edu
Wed Feb 29 18:50:49 CET 2012
Hello everybody. I am stuck with a problem:
===== begin test.py =====
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import fcntl
import multiprocessing
import unittest
N=10000
def lock(fp, mode):
fcntl.flock(fp.fileno(), mode)
def unlock(fp):
fcntl.flock(fp.fileno(), fcntl.LOCK_UN)
def read_write(args):
(filename, iterations) = args
for i in range(0, iterations):
fp = open(filename,'r')
lock(fp,fcntl.LOCK_SH) # shared lock
content = fp.read()
unlock(fp) # unlock
fp.close()
if len(content)!=N:
return False
fp = open(filename,'w')
lock(fp,fcntl.LOCK_EX) # exclusive lock
fp.write(content)
unlock(fp) # unlock
fp.close()
return True
class TestParallelLocks(unittest.TestCase):
def setUp(self):
self.filename = 'test.txt'
contents = 'x'*N
fp = open(self.filename,'w')
fp.write(contents)
fp.close()
def tearDown(self):
try:
os.remove(self.filename)
except:
pass
def test_reads_and_writes(self):
readwriters = 10
pool = multiprocessing.Pool(processes = readwriters)
results = pool.map(read_write, [[self.filename, 10]] *
readwriters)
for result in results:
self.assertTrue(result)
if __name__ == '__main__':
unittest.main()
====== end test.py =====
When I run it, it will often fail the test (on Leopard, Lion, Ubuntu,
Python 2.5, 2.6, 2.7).
Can you reproduce the problem? Do you see anything wrong with the code?
Massimo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20120229/4df38430/attachment.html>
More information about the Chicago
mailing list