gevent.fileobject – Wrappers to make file-like objects cooperative#
Wrappers to make file-like objects cooperative.
- class FileObject(fobj, mode='r', buffering=-1, closefd=True, encoding=None, errors=None, newline=None)#
The main entry point to the file-like gevent-compatible behaviour. It will be defined to be the best available implementation.
All the parameters are as for
Usually a file descriptor of a socket. Can also be another object with a
fileno()method, or an object that can be passed to
io.open()(e.g., a file system path). If the object is not a socket, the results will vary based on the platform and the type of object being opened.
All supported versions of Python allow
Changed in version 1.5: Accept str and
PathLikeobjects for fobj on all versions of Python.
Changed in version 1.5: Add encoding, errors and newline arguments.
Changed in version 1.5: Accept closefd and buffering instead of close and bufsize arguments. The latter remain for backwards compatibility.
There are two main implementations of
FileObject. On all systems,
FileObjectThread which uses the built-in native
threadpool to avoid blocking the entire interpreter. On UNIX systems
(those that support the
fcntl module), there is also
FileObjectPosix which uses native non-blocking semantics.
A third class,
FileObjectBlock, is simply a wrapper that
executes everything synchronously (and so is not gevent-compatible).
It is provided for testing and debugging purposes.
All classes have the same signature; some may accept extra keyword arguments.
You may change the default value for
FileObject using the
GEVENT_FILE environment variable. Set it to
block to choose from
You may also set it to the fully qualified class name of another
object that implements the file interface to use one of your own
The environment variable must be set at the time this module is first imported.
- class FileObjectBlock#
A simple synchronous wrapper around a file object.
Adds no concurrency or gevent compatibility.
- class FileObjectPosix#
A file-like object that operates on non-blocking files but provides a synchronous, cooperative interface.
This object is only effective wrapping files that can be used meaningfully with
select.select()such as sockets and pipes.
In general, on most platforms, operations on regular files (e.g.,
open('a_file.txt')) are considered non-blocking already, even though they can take some time to complete as data is copied to the kernel and flushed to disk: this time is relatively bounded compared to sockets or pipes, though. A
write()call on such a file will still effectively block for some small period of time. Therefore, wrapping this class around a regular file is unlikely to make IO gevent-friendly: reading or writing large amounts of data could still block the event loop.
Although this object provides a
fileno()method and so can itself be passed to
fcntl.fcntl(), setting the
os.O_NONBLOCKflag will have no effect (reads will still block the greenlet, although other greenlets can run). However, removing that flag will cause this object to no longer be cooperative (other greenlets will no longer run).
You can use the internal
fileioattribute of this object (a
io.RawIOBase) to perform non-blocking byte reads. Note, however, that once you begin directly using this attribute, the results from using methods of this object are undefined, especially in text mode. (See issue #222.)
Changed in version 1.1: Now uses the
iopackage internally. Under Python 2, previously used the undocumented class
socket._fileobject. This provides better file-like semantics (and portability to Python 3).
Changed in version 1.2a1: Document the
fileioattribute for non-blocking reads.
Changed in version 1.2a1: A bufsize of 0 in write mode is no longer forced to be 1. Instead, the underlying buffer is flushed after every write operation to simulate a bufsize of 0. In gevent 1.0, a bufsize of 0 was flushed when a newline was written, while in gevent 1.1 it was flushed when more than one byte was written. Note that this may have performance impacts.
Changed in version 1.3a1: On Python 2, enabling universal newlines no longer forces unicode IO.
Changed in version 1.5: Stop forcing buffering. Previously, given a
buffering=0argument, buffering would be set to 1, and
buffering=1would be forced to the default buffer size. This was a workaround for a long-standing concurrency issue. Now the buffering argument is interpreted as intended.
- class FileObjectThread#
A file-like object wrapping another file-like object, performing all blocking operations on that object in a background thread.
Attempting to change the threadpool or lock of an existing FileObjectThread has undefined consequences.
Changed in version 1.1b1: The file object is closed using the threadpool. Note that whether or not this action is synchronous or asynchronous is not documented.
lock (bool) – If True (the default) then all operations will be performed one-by-one. Note that this does not guarantee that, if using this file object from multiple threads/greenlets, operations will be performed in any particular order, only that no two operations will be attempted at the same time. You can also pass your own
gevent.lock.Semaphoreto synchronize file operations with an external resource.
closefd (bool) – If True (the default) then when this object is closed, the underlying object is closed as well. If fobj is a path, then closefd must be True.