Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:8.13.1 Template ObjectsUp:8. Unix Specific ServicesNext:8.15 resource

8.14posixfile -- File-like objects with locking support

Availability:Unix.

 

Deprecated since release 1.5.The locking operation that this module provides isdone better and more portably by thefcntl.lockf() call. 

This module implements some additional functionality over the built-infile objects. In particular, it implements file locking, control overthe file flags, and an easy interface to duplicate the file object.The module defines a new file object, the posixfile object. Ithas all the standard file object methods and adds the methodsdescribed below. This module only works for certain flavors ofUnix, since it usesfcntl.fcntl() for file locking. 

To instantiate a posixfile object, use theopen() functionin theposixfile module. The resulting object looks andfeels roughly the same as a standard file object.

Theposixfile module defines the following constants:

SEEK_SET
Offset is calculated from the start of the file.

SEEK_CUR
Offset is calculated from the current position in the file.

SEEK_END
Offset is calculated from the end of the file.

Theposixfile module defines the following functions:

open(filename[, mode[, bufsize]])
Create a new posixfile object with the given filename and mode. Thefilename,mode andbufsize arguments are interpreted the same way as by the built-inopen() function.

fileopen(fileobject)
Create a new posixfile object with the given standard file object. The resulting object has the same filename and mode as the original file object.

The posixfile object defines the following additional methods:

lock(fmt,[len[, start[, whence]]])
Lock the specified section of the file that the file object is referring to. The format is explained below in a table. Thelen argument specifies the length of the section that should be locked. The default is0.start specifies the starting offset of the section, where the default is0. Thewhence argument specifies where the offset is relative to. It accepts one of the constantsSEEK_SET,SEEK_CUR orSEEK_END. The default isSEEK_SET. For more information about the arguments refer to thefcntl(2) manual page on your system.

flags([flags])
Set the specified flags for the file that the file object is referring to. The new flags are ORed with the old flags, unless specified otherwise. The format is explained below in a table. Without theflags argument a string indicating the current flags is returned (this is the same as the "?" modifier). For more information about the flags refer to thefcntl(2) manual page on your system.

dup()
Duplicate the file object and the underlying file pointer and file descriptor. The resulting object behaves as if it were newly opened.

dup2(fd)
Duplicate the file object and the underlying file pointer and file descriptor. The new object will have the given file descriptor. Otherwise the resulting object behaves as if it were newly opened.

file()
Return the standard file object that the posixfile object is based on. This is sometimes necessary for functions that insist on a standard file object.

All methods raiseIOError when the request fails.

Format characters for thelock() method have the followingmeaning:

Format Meaning 
uunlock the specified region
rrequest a read lock for the specified section
wrequest a write lock for the specified section

In addition the following modifiers can be added to the format:

Modifier Meaning Notes 
|wait until the lock has been granted 
?return the first lock conflicting with the requested lock, orNone if there is no conflict.(1)

Note:

(1)
The lock returned is in the format(mode,len,start,whence,pid) wheremode is a characterrepresenting the type of lock ('r' or 'w'). This modifier prevents arequest from being granted; it is for query purposes only.

Format characters for theflags() method have the followingmeanings:

Format Meaning 
aappend only flag
cclose on exec flag
nno delay flag (also called non-blocking flag)
ssynchronization flag

In addition the following modifiers can be added to the format:

Modifier Meaning Notes 
!turn the specified flags 'off', instead of the default 'on'(1)
=replace the flags, instead of the default 'OR' operation(1)
?return a string in which the characters represent the flags that are set.(2)

Notes:

(1)
The "!" and "=" modifiers are mutually exclusive.

(2)
This string represents the flags after they may have been alteredby the same call.

Examples:

import posixfilefile = posixfile.open('/tmp/test', 'w')file.lock('w|')...file.lock('u')file.close()


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:8.13.1 Template ObjectsUp:8. Unix Specific ServicesNext:8.15 resource
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp