gevent.fileobject – Wrappers to make file-like objects cooperative

class FileObject

The main entry point to the file-like gevent-compatible behaviour. It will be defined to be the best available implementation.

There are two main implementations of FileObject. On all systems, there is 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.

Configuration

You may change the default value for FileObject using the GEVENT_FILE environment variable. Set it to posix, thread, or block to choose from FileObjectPosix, FileObjectThread and FileObjectBlock, respectively. 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 objects.

Note

The environment variable must be set at the time this module is first imported.

class FileObjectPosix(fobj, mode='rb', bufsize=-1, close=True)

Bases: object

A file-like object that operates on non-blocking files.

See also

gevent.os.make_nonblocking()

Parameters:fobj – Either an integer fileno, or an object supporting the usual socket.fileno() method. The file will be put in non-blocking mode.
closed

True if the file is cloed

default_bufsize = 8192
close()
flush()
fileno()
write(data)
writelines(lines)
read(size=-1)
readline(size=-1)
readlines(sizehint=0)
readable()
writable()
seek(*args, **kwargs)
seekable()
tell()
truncate(size=None)
class FileObjectThread(fobj, *args, **kwargs)

Bases: object

A file-like object wrapping another file-like object, performing all blocking operations on that object in a background thread.

Parameters:
  • fobj – The underlying file-like object to wrap, or an integer fileno that will be pass to os.fdopen() along with everything in args.
  • 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.Semaphore to synchronize file operations with an external resource.
  • close (bool) – If True (the default) then when this object is closed, the underlying object is closed as well.
close()
flush(_fobj=None)
next()
read(*args, **kwargs)
readinto(*args, **kwargs)
readline(*args, **kwargs)
readlines(*args, **kwargs)
write(*args, **kwargs)
writelines(*args, **kwargs)
xreadlines(*args, **kwargs)
FileObject

alias of FileObjectPosix

Next page: gevent.local – Greenlet-local objects