Mercurial > p > roundup > code
annotate roundup/backends/portalocker.py @ 1459:7e0bbefb2a72
reversing the "make it nicer" checkin
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 20 Feb 2003 22:56:49 +0000 |
| parents | 1159963f769c |
| children | eeb167fb8faf |
| rev | line source |
|---|---|
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1 # portalocker.py - Cross-platform (posix/nt) API for flock-style file locking. |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
2 # Requires python 1.5.2 or better. |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
3 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
4 # ID line added by richard for Roundup file tracking |
|
1459
7e0bbefb2a72
reversing the "make it nicer" checkin
Richard Jones <richard@users.sourceforge.net>
parents:
1430
diff
changeset
|
5 # $Id: portalocker.py,v 1.5 2003-02-20 22:56:49 richard Exp $ |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
6 |
|
1244
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
690
diff
changeset
|
7 """ Cross-platform (posix/nt) API for flock-style file locking. |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
8 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
9 Synopsis: |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
10 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
11 import portalocker |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
12 file = open("somefile", "r+") |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
13 portalocker.lock(file, portalocker.LOCK_EX) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
14 file.seek(12) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
15 file.write("foo") |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
16 file.close() |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
17 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
18 If you know what you're doing, you may choose to |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
19 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
20 portalocker.unlock(file) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
21 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
22 before closing the file, but why? |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
23 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
24 Methods: |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
25 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
26 lock( file, flags ) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
27 unlock( file ) |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
28 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
29 Constants: |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
30 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
31 LOCK_EX |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
32 LOCK_SH |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
33 LOCK_NB |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
34 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
35 I learned the win32 technique for locking files from sample code |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
36 provided by John Nielsen <nielsenjf@my-deja.com> in the documentation |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
37 that accompanies the win32 modules. |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
38 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
39 Author: Jonathan Feinberg <jdf@pobox.com> |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
40 Version: Id: portalocker.py,v 1.3 2001/05/29 18:47:55 Administrator Exp |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
41 **un-cvsified by richard so the version doesn't change** |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
42 """ |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
43 import os |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
44 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
45 if os.name == 'nt': |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
46 import win32con |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
47 import win32file |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
48 import pywintypes |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
49 LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
50 LOCK_SH = 0 # the default |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
51 LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
52 # is there any reason not to reuse the following structure? |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
53 __overlapped = pywintypes.OVERLAPPED() |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
54 elif os.name == 'posix': |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
55 import fcntl |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
56 LOCK_EX = fcntl.LOCK_EX |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
57 LOCK_SH = fcntl.LOCK_SH |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
58 LOCK_NB = fcntl.LOCK_NB |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
59 else: |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
60 raise RuntimeError("PortaLocker only defined for nt and posix platforms") |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
61 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
62 if os.name == 'nt': |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
63 def lock(file, flags): |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
64 hfile = win32file._get_osfhandle(file.fileno()) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
65 # LockFileEx is not supported on all Win32 platforms (Win95, Win98, WinME). |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
66 # If it's not supported, win32file will raise an exception. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
67 # Try LockFileEx first, as it has more functionality and handles |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
68 # blocking locks more efficiently. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
69 try: |
|
1459
7e0bbefb2a72
reversing the "make it nicer" checkin
Richard Jones <richard@users.sourceforge.net>
parents:
1430
diff
changeset
|
70 win32file.LockFileEx(hfile, flags, 0, 0xffff0000, __overlapped) |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
71 except win32file.error, e: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
72 import winerror |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
73 # Propagate upwards all exceptions other than not-implemented. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
74 if e[0] != winerror.ERROR_CALL_NOT_IMPLEMENTED: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
75 raise e |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
76 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
77 # LockFileEx is not supported. Use LockFile. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
78 # LockFile does not support shared locking -- always exclusive. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
79 # Care: the low/high length params are reversed compared to LockFileEx. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
80 if not flags & LOCK_EX: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
81 import warnings |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
82 warnings.warn("PortaLocker does not support shared locking on Win9x", RuntimeWarning) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
83 # LockFile only supports immediate-fail locking. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
84 if flags & LOCK_NB: |
|
1459
7e0bbefb2a72
reversing the "make it nicer" checkin
Richard Jones <richard@users.sourceforge.net>
parents:
1430
diff
changeset
|
85 win32file.LockFile(hfile, 0, 0, 0xffff0000, 0) |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
86 else: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
87 # Emulate a blocking lock with a polling loop. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
88 import time |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
89 while 1: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
90 # Attempt a lock. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
91 try: |
|
1459
7e0bbefb2a72
reversing the "make it nicer" checkin
Richard Jones <richard@users.sourceforge.net>
parents:
1430
diff
changeset
|
92 win32file.LockFile(hfile, 0, 0, 0xffff0000, 0) |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
93 break |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
94 except win32file.error, e: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
95 # Propagate upwards all exceptions other than lock violation. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
96 if e[0] != winerror.ERROR_LOCK_VIOLATION: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
97 raise e |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
98 # Sleep and poll again. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
99 time.sleep(0.1) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
100 # TODO: should this return the result of the lock? |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
101 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
102 def unlock(file): |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
103 hfile = win32file._get_osfhandle(file.fileno()) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
104 # UnlockFileEx is not supported on all Win32 platforms (Win95, Win98, WinME). |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
105 # If it's not supported, win32file will raise an api_error exception. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
106 try: |
|
1459
7e0bbefb2a72
reversing the "make it nicer" checkin
Richard Jones <richard@users.sourceforge.net>
parents:
1430
diff
changeset
|
107 win32file.UnlockFileEx(hfile, 0, 0xffff0000, __overlapped) |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
108 except win32file.error, e: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
109 import winerror |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
110 # Propagate upwards all exceptions other than not-implemented. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
111 if e[0] != winerror.ERROR_CALL_NOT_IMPLEMENTED: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
112 raise e |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
113 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
114 # UnlockFileEx is not supported. Use UnlockFile. |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
115 # Care: the low/high length params are reversed compared to UnLockFileEx. |
|
1459
7e0bbefb2a72
reversing the "make it nicer" checkin
Richard Jones <richard@users.sourceforge.net>
parents:
1430
diff
changeset
|
116 win32file.UnlockFile(hfile, 0, 0, 0xffff0000, 0) |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
117 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
118 elif os.name =='posix': |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
119 def lock(file, flags): |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
120 fcntl.flock(file.fileno(), flags) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
121 # TODO: should this return the result of the lock? |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
122 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
123 def unlock(file): |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
124 fcntl.flock(file.fileno(), fcntl.LOCK_UN) |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
125 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
126 if __name__ == '__main__': |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
127 from time import time, strftime, localtime |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
128 import sys |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
129 import portalocker |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
130 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
131 log = open('log.txt', "a+") |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
132 portalocker.lock(log, portalocker.LOCK_EX) |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
133 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
134 timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time())) |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
135 log.write( timestamp ) |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
136 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
137 print "Wrote lines. Hit enter to release lock." |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
138 dummy = sys.stdin.readline() |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
139 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
140 log.close() |
|
690
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
141 |
