Giter VIP home page Giter VIP logo

pystdoutwrapper's Introduction

StdoutWrapper

A simple Python wrapper for running programs as subprocesses in a new thread while convieniently accessing their stdout.

This is intended for accessing the stdout of a program while the program is still executing. For example, when a program outputs live/realtime data that you want to handle in a Python script.

Example

As an example theres the pre-compiled program count_up.exe included in this repository. count_up.exe starts at 0 and outputs the next integer with a one second delay.

> count_up.exe
0
1
2
3
...

To capture this programs output, the following example code can be used.

from stdoutWrapper import StdoutWrapper
from time import sleep

# callback method, put the process output into a buffer array
buff_size = 10
buff = []
def callback(stdout_line: str):
    global buff
    buff.append(int(stdout_line))
    buff = buff[-buff_size:]


# create a StdoutWrapper instance, when count_up.exe outputs a new line on stdout, the callback will be called
stw = StdoutWrapper('./count_up.exe', callback_output=callback)
# start the execution, this will be done in a new thread, not blocking the current thread
stw.start()
while True:
    try:
        sleep(1)
        print(buff)
    except KeyboardInterrupt:
        break
stw.stop()

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.