Mercurial > p > roundup > code
annotate roundup/backends/portalocker.py @ 1755:0e123e7c6ddc
fixed 0xffff0000 literal. finally.
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Tue, 26 Aug 2003 00:29:20 +0000 |
| parents | eeb167fb8faf |
| children | fc52d57c6c3e |
| 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 |
|
1755
0e123e7c6ddc
fixed 0xffff0000 literal. finally.
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
5 # $Id: portalocker.py,v 1.7 2003-08-26 00:29:20 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': |
|
1755
0e123e7c6ddc
fixed 0xffff0000 literal. finally.
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
63 # eugh, we want 0xffff0000 here, but python 2.3 won't let us :( |
|
0e123e7c6ddc
fixed 0xffff0000 literal. finally.
Richard Jones <richard@users.sourceforge.net>
parents:
1719
diff
changeset
|
64 FFFF0000 = -65536 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
65 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
|
66 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
|
67 # 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
|
68 # 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
|
69 # 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
|
70 # 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
|
71 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
72 win32file.LockFileEx(hfile, flags, 0, FFFF0000, __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
|
73 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
|
74 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
|
75 # 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
|
76 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
|
77 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
|
78 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
79 # 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
|
80 # 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
|
81 # 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
|
82 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
|
83 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
|
84 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
|
85 # 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
|
86 if flags & LOCK_NB: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
87 win32file.LockFile(hfile, 0, 0, FFFF0000, 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
|
88 else: |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
89 # 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
|
90 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
|
91 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
|
92 # 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
|
93 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
94 win32file.LockFile(hfile, 0, 0, FFFF0000, 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
|
95 break |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
96 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
|
97 # 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
|
98 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
|
99 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
|
100 # 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
|
101 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
|
102 # 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
|
103 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
104 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
|
105 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
|
106 # 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
|
107 # 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
|
108 try: |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
109 win32file.UnlockFileEx(hfile, 0, FFFF0000, __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
|
110 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
|
111 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
|
112 # 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
|
113 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
|
114 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
|
115 |
|
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
116 # 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
|
117 # Care: the low/high length params are reversed compared to UnLockFileEx. |
|
1719
eeb167fb8faf
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
1459
diff
changeset
|
118 win32file.UnlockFile(hfile, 0, 0, FFFF0000, 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
|
119 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
120 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
|
121 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
|
122 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
|
123 # 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
|
124 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
125 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
|
126 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
|
127 |
|
509a101305da
node ids are now generated from a lockable store - no more race conditions
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
128 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
|
129 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
|
130 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
|
131 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
|
132 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
133 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
|
134 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
|
135 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
136 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
|
137 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
|
138 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
139 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
|
140 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
|
141 |
|
1387
e975db910d9f
latest version of portalocker fixed for win98 and winnt, thanks James Kew
Richard Jones <richard@users.sourceforge.net>
parents:
1244
diff
changeset
|
142 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
|
143 |
