[Chicago] help with file locking
Massimo Di Pierro
mdipierro at cs.depaul.edu
Wed Feb 29 19:43:56 CET 2012
Thank you! This helps a lot.
Massimo
On Feb 29, 2012, at 12:38 PM, Dan McGee wrote:
> On Wed, Feb 29, 2012 at 12:28 PM, Dan McGee <dpmcgee at gmail.com> wrote:
>> On Wed, Feb 29, 2012 at 11:50 AM, Massimo Di Pierro
>> <mdipierro at cs.depaul.edu> wrote:
>>> Hello everybody. I am stuck with a problem:
>
>> lock(fp, fcntl.LOCK_EX, pn) # exclusive lock
>
> That new 'pn' argument came from some debugging I had in to track the
> process pool number. Full revised script code below.
>
> -Dan
>
>
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
> import fcntl
> import multiprocessing
> import unittest
>
> N=10000
>
> def lock(fp, mode, pn):
> fcntl.flock(fp, mode)
>
> def unlock(fp, pn):
> fcntl.flock(fp, fcntl.LOCK_UN)
>
> def read_write(args):
> (filename, iterations, pn) = args
> for i in range(0, iterations):
> fp = open(filename, 'r')
> lock(fp, fcntl.LOCK_SH, pn) # shared lock
> content = fp.read()
> unlock(fp, pn) # unlock
> fp.close()
> if len(content) != N:
> return len(content)
> fp = open(filename, 'a')
> lock(fp, fcntl.LOCK_EX, pn) # exclusive lock
> fp.seek(0)
> fp.truncate()
> fp.write(content)
> fp.flush()
> unlock(fp, pn) # unlock
> fp.close()
> return len(content)
>
> 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)
> args = [[self.filename, 10, i] for i in range(readwriters)]
> results = pool.map(read_write, args)
> for result in results:
> self.assertEqual(N, result)
>
> if __name__ == '__main__':
> unittest.main(verbosity=2)
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
More information about the Chicago
mailing list