Giter Site home page Giter Site logo

asynchronousfilereader's Introduction

AsynchronousFileReader

Simple thread based asynchronous file reader for Python.

Canonical example use case: you running a longer running child process with the subprocess module and want to monitor its standard output and error along the way. A tricky issue with this kind of parallel "flows" is the risk on deadlocks if you are not careful with the order things are done. For example, you want to read from the subprocess standard output pipe, but the buffer of the standard error pipe is full and the operating system wants you to read that first. Kaboom, deadlock.

If it's ok to get the complete standard output and error content in one fell swoop, the communicate() method, as recommended by the subprocess.Popen documentation, is the way to go. However, if you want to monitor the streams line by line, you need other tricks. On the web you can find many solutions, with varying degrees of complexity, abstraction and dependencies.

AsynchronousFileReader provides a simple solution with limited code and no dependencies outside the standard library. It reads the files or pipes line by line in separate threads (so one pipe can't block another) and allows the main thread to fetch these lines as they become available.

Example/Demo

The script demo.py provides an example of how to use it. The script acts both as "child" process (producer mode) and monitoring process (consume mode). Just run it without arguments and watch it go.

Basically, the usage pattern is as follows:

# `file` is a file-like object (it just has to provide a readline() method actually)
reader = AsynchronousFileReader(file)
while not reader.eof():
    for line in reader.readlines():
        # Do something with `line` here
        store(line)

    # Do something else in the meantime while waiting for more input.
    other_time_consuming_stuff()

# Be tidy and join the thread.
reader.join()

Features

  • supports both Python 2.x and 3.x
  • only depends on standard library (threading and Queue)
  • simple, one file implementation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.