| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-resource.html" /> |
| 13 | <link rel="prev" href="module-pipes.html" /> |
| 14 | <link rel="parent" href="unix.html" /> |
| 15 | <link rel="next" href="module-resource.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>8.13 posixfile -- File-like objects with locking support</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="8.12.1 Template Objects" |
| 25 | href="template-objects.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="8. Unix Specific Services" |
| 28 | href="unix.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="8.14 resource " |
| 31 | href="module-resource.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="template-objects.html">8.12.1 Template Objects</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="unix.html">8. Unix Specific Services</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="module-resource.html">8.14 resource </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION00101300000000000000000"> |
| 56 | 8.13 <tt class="module">posixfile</tt> -- |
| 57 | File-like objects with locking support</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-posixfile"></A> |
| 62 | <p class="availability">Availability: <span |
| 63 | class="platform">Unix</span>.</p> |
| 64 | |
| 65 | <P> |
| 66 | <a id='l2h-3040' xml:id='l2h-3040'></a> |
| 67 | <P> |
| 68 | <div class="versionnote"><b>Deprecated since release 1.5.</b> |
| 69 | The locking operation that this module provides is |
| 70 | done better and more portably by the |
| 71 | <tt class="function"><tt class="module"><a href="module-fcntl.html">fcntl</a></tt>.lockf()</tt> call. |
| 72 | <a id='l2h-3042' xml:id='l2h-3042'></a></div><p></p> |
| 73 | |
| 74 | <P> |
| 75 | This module implements some additional functionality over the built-in |
| 76 | file objects. In particular, it implements file locking, control over |
| 77 | the file flags, and an easy interface to duplicate the file object. |
| 78 | The module defines a new file object, the posixfile object. It |
| 79 | has all the standard file object methods and adds the methods |
| 80 | described below. This module only works for certain flavors of |
| 81 | <span class="Unix">Unix</span>, since it uses <tt class="function">fcntl.fcntl()</tt> for file locking.<a id='l2h-3044' xml:id='l2h-3044'></a> |
| 82 | <P> |
| 83 | To instantiate a posixfile object, use the <tt class="function">open()</tt> function |
| 84 | in the <tt class="module">posixfile</tt> module. The resulting object looks and |
| 85 | feels roughly the same as a standard file object. |
| 86 | |
| 87 | <P> |
| 88 | The <tt class="module">posixfile</tt> module defines the following constants: |
| 89 | |
| 90 | <P> |
| 91 | <dl><dt><b><tt id='l2h-3045' xml:id='l2h-3045'>SEEK_SET</tt></b></dt> |
| 92 | <dd> |
| 93 | Offset is calculated from the start of the file. |
| 94 | </dd></dl> |
| 95 | |
| 96 | <P> |
| 97 | <dl><dt><b><tt id='l2h-3046' xml:id='l2h-3046'>SEEK_CUR</tt></b></dt> |
| 98 | <dd> |
| 99 | Offset is calculated from the current position in the file. |
| 100 | </dd></dl> |
| 101 | |
| 102 | <P> |
| 103 | <dl><dt><b><tt id='l2h-3047' xml:id='l2h-3047'>SEEK_END</tt></b></dt> |
| 104 | <dd> |
| 105 | Offset is calculated from the end of the file. |
| 106 | </dd></dl> |
| 107 | |
| 108 | <P> |
| 109 | The <tt class="module">posixfile</tt> module defines the following functions: |
| 110 | |
| 111 | <P> |
| 112 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 113 | <td><nobr><b><tt id='l2h-3048' xml:id='l2h-3048' class="function">open</tt></b>(</nobr></td> |
| 114 | <td><var>filename</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 115 | <dd> |
| 116 | Create a new posixfile object with the given filename and mode. The |
| 117 | <var>filename</var>, <var>mode</var> and <var>bufsize</var> arguments are |
| 118 | interpreted the same way as by the built-in <tt class="function">open()</tt> |
| 119 | function. |
| 120 | </dl> |
| 121 | |
| 122 | <P> |
| 123 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 124 | <td><nobr><b><tt id='l2h-3049' xml:id='l2h-3049' class="function">fileopen</tt></b>(</nobr></td> |
| 125 | <td><var>fileobject</var>)</td></tr></table></dt> |
| 126 | <dd> |
| 127 | Create a new posixfile object with the given standard file object. |
| 128 | The resulting object has the same filename and mode as the original |
| 129 | file object. |
| 130 | </dl> |
| 131 | |
| 132 | <P> |
| 133 | The posixfile object defines the following additional methods: |
| 134 | |
| 135 | <P> |
| 136 | |
| 137 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 138 | <td><nobr><b><tt id='l2h-3050' xml:id='l2h-3050' class="function">lock</tt></b>(</nobr></td> |
| 139 | <td><var>fmt, </var><big>[</big><var>len</var><big>[</big><var>, start</var><big>[</big><var>, whence</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 140 | <dd> |
| 141 | Lock the specified section of the file that the file object is |
| 142 | referring to. The format is explained |
| 143 | below in a table. The <var>len</var> argument specifies the length of the |
| 144 | section that should be locked. The default is <code>0</code>. <var>start</var> |
| 145 | specifies the starting offset of the section, where the default is |
| 146 | <code>0</code>. The <var>whence</var> argument specifies where the offset is |
| 147 | relative to. It accepts one of the constants <tt class="constant">SEEK_SET</tt>, |
| 148 | <tt class="constant">SEEK_CUR</tt> or <tt class="constant">SEEK_END</tt>. The default is |
| 149 | <tt class="constant">SEEK_SET</tt>. For more information about the arguments refer |
| 150 | to the <span class="manpage"><i>fcntl</i>(2)</span> manual page on your system. |
| 151 | </dl> |
| 152 | |
| 153 | <P> |
| 154 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 155 | <td><nobr><b><tt id='l2h-3051' xml:id='l2h-3051' class="function">flags</tt></b>(</nobr></td> |
| 156 | <td><var></var><big>[</big><var>flags</var><big>]</big><var></var>)</td></tr></table></dt> |
| 157 | <dd> |
| 158 | Set the specified flags for the file that the file object is referring |
| 159 | to. The new flags are ORed with the old flags, unless specified |
| 160 | otherwise. The format is explained below in a table. Without |
| 161 | the <var>flags</var> argument |
| 162 | a string indicating the current flags is returned (this is |
| 163 | the same as the "<tt class="samp">?</tt>" modifier). For more information about the |
| 164 | flags refer to the <span class="manpage"><i>fcntl</i>(2)</span> manual page on your system. |
| 165 | </dl> |
| 166 | |
| 167 | <P> |
| 168 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 169 | <td><nobr><b><tt id='l2h-3052' xml:id='l2h-3052' class="function">dup</tt></b>(</nobr></td> |
| 170 | <td><var></var>)</td></tr></table></dt> |
| 171 | <dd> |
| 172 | Duplicate the file object and the underlying file pointer and file |
| 173 | descriptor. The resulting object behaves as if it were newly |
| 174 | opened. |
| 175 | </dl> |
| 176 | |
| 177 | <P> |
| 178 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 179 | <td><nobr><b><tt id='l2h-3053' xml:id='l2h-3053' class="function">dup2</tt></b>(</nobr></td> |
| 180 | <td><var>fd</var>)</td></tr></table></dt> |
| 181 | <dd> |
| 182 | Duplicate the file object and the underlying file pointer and file |
| 183 | descriptor. The new object will have the given file descriptor. |
| 184 | Otherwise the resulting object behaves as if it were newly opened. |
| 185 | </dl> |
| 186 | |
| 187 | <P> |
| 188 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 189 | <td><nobr><b><tt id='l2h-3054' xml:id='l2h-3054' class="function">file</tt></b>(</nobr></td> |
| 190 | <td><var></var>)</td></tr></table></dt> |
| 191 | <dd> |
| 192 | Return the standard file object that the posixfile object is based |
| 193 | on. This is sometimes necessary for functions that insist on a |
| 194 | standard file object. |
| 195 | </dl> |
| 196 | |
| 197 | <P> |
| 198 | All methods raise <tt class="exception">IOError</tt> when the request fails. |
| 199 | |
| 200 | <P> |
| 201 | Format characters for the <tt class="method">lock()</tt> method have the following |
| 202 | meaning: |
| 203 | |
| 204 | <P> |
| 205 | <div class="center"><table class="realtable"> |
| 206 | <thead> |
| 207 | <tr> |
| 208 | <th class="center">Format</th> |
| 209 | <th class="left" >Meaning</th> |
| 210 | </tr> |
| 211 | </thead> |
| 212 | <tbody> |
| 213 | <tr><td class="center" valign="baseline"><samp>u</samp></td> |
| 214 | <td class="left" >unlock the specified region</td></tr> |
| 215 | <tr><td class="center" valign="baseline"><samp>r</samp></td> |
| 216 | <td class="left" >request a read lock for the specified section</td></tr> |
| 217 | <tr><td class="center" valign="baseline"><samp>w</samp></td> |
| 218 | <td class="left" >request a write lock for the specified section</td></tr></tbody> |
| 219 | </table></div> |
| 220 | |
| 221 | <P> |
| 222 | In addition the following modifiers can be added to the format: |
| 223 | |
| 224 | <P> |
| 225 | <div class="center"><table class="realtable"> |
| 226 | <thead> |
| 227 | <tr> |
| 228 | <th class="center">Modifier</th> |
| 229 | <th class="left" >Meaning</th> |
| 230 | <th class="center">Notes</th> |
| 231 | </tr> |
| 232 | </thead> |
| 233 | <tbody> |
| 234 | <tr><td class="center" valign="baseline"><samp>|</samp></td> |
| 235 | <td class="left" >wait until the lock has been granted</td> |
| 236 | <td class="center"></td></tr> |
| 237 | <tr><td class="center" valign="baseline"><samp>?</samp></td> |
| 238 | <td class="left" >return the first lock conflicting with the requested lock, or |
| 239 | <code>None</code> if there is no conflict.</td> |
| 240 | <td class="center">(1)</td></tr></tbody> |
| 241 | </table></div> |
| 242 | |
| 243 | <P> |
| 244 | Note: |
| 245 | |
| 246 | <P> |
| 247 | <DL> |
| 248 | <DT><STRONG>(1)</STRONG></DT> |
| 249 | <DD>The lock returned is in the format <code>(<var>mode</var>, <var>len</var>, |
| 250 | <var>start</var>, <var>whence</var>, <var>pid</var>)</code> where <var>mode</var> is a character |
| 251 | representing the type of lock ('r' or 'w'). This modifier prevents a |
| 252 | request from being granted; it is for query purposes only. |
| 253 | </DD> |
| 254 | </DL> |
| 255 | |
| 256 | <P> |
| 257 | Format characters for the <tt class="method">flags()</tt> method have the following |
| 258 | meanings: |
| 259 | |
| 260 | <P> |
| 261 | <div class="center"><table class="realtable"> |
| 262 | <thead> |
| 263 | <tr> |
| 264 | <th class="center">Format</th> |
| 265 | <th class="left" >Meaning</th> |
| 266 | </tr> |
| 267 | </thead> |
| 268 | <tbody> |
| 269 | <tr><td class="center" valign="baseline"><samp>a</samp></td> |
| 270 | <td class="left" >append only flag</td></tr> |
| 271 | <tr><td class="center" valign="baseline"><samp>c</samp></td> |
| 272 | <td class="left" >close on exec flag</td></tr> |
| 273 | <tr><td class="center" valign="baseline"><samp>n</samp></td> |
| 274 | <td class="left" >no delay flag (also called non-blocking flag)</td></tr> |
| 275 | <tr><td class="center" valign="baseline"><samp>s</samp></td> |
| 276 | <td class="left" >synchronization flag</td></tr></tbody> |
| 277 | </table></div> |
| 278 | |
| 279 | <P> |
| 280 | In addition the following modifiers can be added to the format: |
| 281 | |
| 282 | <P> |
| 283 | <div class="center"><table class="realtable"> |
| 284 | <thead> |
| 285 | <tr> |
| 286 | <th class="center">Modifier</th> |
| 287 | <th class="left" >Meaning</th> |
| 288 | <th class="center">Notes</th> |
| 289 | </tr> |
| 290 | </thead> |
| 291 | <tbody> |
| 292 | <tr><td class="center" valign="baseline"><samp>!</samp></td> |
| 293 | <td class="left" >turn the specified flags 'off', instead of the default 'on'</td> |
| 294 | <td class="center">(1)</td></tr> |
| 295 | <tr><td class="center" valign="baseline"><samp>=</samp></td> |
| 296 | <td class="left" >replace the flags, instead of the default 'OR' operation</td> |
| 297 | <td class="center">(1)</td></tr> |
| 298 | <tr><td class="center" valign="baseline"><samp>?</samp></td> |
| 299 | <td class="left" >return a string in which the characters represent the flags that |
| 300 | are set.</td> |
| 301 | <td class="center">(2)</td></tr></tbody> |
| 302 | </table></div> |
| 303 | |
| 304 | <P> |
| 305 | Notes: |
| 306 | |
| 307 | <P> |
| 308 | <DL> |
| 309 | <DT><STRONG>(1)</STRONG></DT> |
| 310 | <DD>The "<tt class="samp">!</tt>" and "<tt class="samp">=</tt>" modifiers are mutually exclusive. |
| 311 | |
| 312 | <P> |
| 313 | </DD> |
| 314 | <DT><STRONG>(2)</STRONG></DT> |
| 315 | <DD>This string represents the flags after they may have been altered |
| 316 | by the same call. |
| 317 | </DD> |
| 318 | </DL> |
| 319 | |
| 320 | <P> |
| 321 | Examples: |
| 322 | |
| 323 | <P> |
| 324 | <div class="verbatim"><pre> |
| 325 | import posixfile |
| 326 | |
| 327 | file = posixfile.open('/tmp/test', 'w') |
| 328 | file.lock('w|') |
| 329 | ... |
| 330 | file.lock('u') |
| 331 | file.close() |
| 332 | </pre></div> |
| 333 | |
| 334 | <DIV CLASS="navigation"> |
| 335 | <div class='online-navigation'> |
| 336 | <p></p><hr /> |
| 337 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 338 | <tr> |
| 339 | <td class='online-navigation'><a rel="prev" title="8.12.1 Template Objects" |
| 340 | href="template-objects.html"><img src='../icons/previous.png' |
| 341 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 342 | <td class='online-navigation'><a rel="parent" title="8. Unix Specific Services" |
| 343 | href="unix.html"><img src='../icons/up.png' |
| 344 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 345 | <td class='online-navigation'><a rel="next" title="8.14 resource " |
| 346 | href="module-resource.html"><img src='../icons/next.png' |
| 347 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 348 | <td align="center" width="100%">Python Library Reference</td> |
| 349 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 350 | href="contents.html"><img src='../icons/contents.png' |
| 351 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 352 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 353 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 354 | <td class='online-navigation'><a rel="index" title="Index" |
| 355 | href="genindex.html"><img src='../icons/index.png' |
| 356 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 357 | </tr></table> |
| 358 | <div class='online-navigation'> |
| 359 | <b class="navlabel">Previous:</b> |
| 360 | <a class="sectref" rel="prev" href="template-objects.html">8.12.1 Template Objects</A> |
| 361 | <b class="navlabel">Up:</b> |
| 362 | <a class="sectref" rel="parent" href="unix.html">8. Unix Specific Services</A> |
| 363 | <b class="navlabel">Next:</b> |
| 364 | <a class="sectref" rel="next" href="module-resource.html">8.14 resource </A> |
| 365 | </div> |
| 366 | </div> |
| 367 | <hr /> |
| 368 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 369 | </DIV> |
| 370 | <!--End of Navigation Panel--> |
| 371 | <ADDRESS> |
| 372 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 373 | </ADDRESS> |
| 374 | </BODY> |
| 375 | </HTML> |