File IO

A Futures-based file IO library using the Tornado concurrent.run_on_executor decorator. Requires either Python >= 3.2 or the futures package from pip.

Tornado applications should be able to use it directly in a gen.coroutine decorator, like so:

from iceprod.server.file_io import AsyncFileIO
class MyHandler(tornado.web.RequestHandler):
    @tornado.gen.coroutine
    def get(self):
        io = AsyncFileIO()
        file = yield io.open('my_file.txt')
        data = yield io.read(file)
        self.write(data)
        yield AsyncFileIO.close(file)
class iceprod.server.file_io.AsyncFileIO(executor=None, io_loop=None)[source]

Async File IO hidden behind threads using concurrent.futures.

Parameters:
  • executor – A concurrent.futures.Executor object, optional. Defaults to a 1 thread Executor.
  • io_loop – A tornado.ioloop.IOLoop object, optional. Defaults to the current IOLoop.
open(name, mode=None)[source]

Open a file.

Parameters:
  • filename – Filename of a file.
  • mode – Mode to open a file in, optional.
Returns:

file object.

Raises:

IOError if file cannot be opened.

read(file, bytes=65536)[source]

Read from an open file object.

Parameters:
  • file – Open file object.
  • bytes – Number of bytes to read, defaults to 64k.
Returns:

Data read from file.

Raises:

IOError if file cannot be read from.

readline(file)[source]

Read a line from an open file object.

Parameters:file – Open file object.
Returns:A line from the file.
Raises:IOError if file cannot be read from.
write(file, data)[source]

Write some data to an open file object.

Parameters:
  • file – Open file object.
  • data – Some data to write.
Raises:

IOError if file cannot be written to.

close(file)[source]

Close an open file object.

Parameters:file – Open file object.
Raises:IOError if file cannot be closed.