Giter VIP home page Giter VIP logo

subpiper's Introduction

subpiper

Subprocess wrapper for separate, unbuffered capturing / redirecting of stdout and stderr

Usage

Blocking mode

from subpiper import subpiper

def my_stdout_callback(line: str):
    # do whatever you like with stdout lines
    print(f'STDOUT: {line}')

def my_stderr_callback(line: str):
    # do whatever you like with stderr lines
    print(f'STDERR: {line}')

# add some path to the PATH for your subprocess
my_additional_path_list = [r'c:\important_location']

# call subpiper to take care everyting in blocking mode
retcode, *_ = subpiper(
    cmd='echo magic',
    stdout_callback=my_stdout_callback,
    stderr_callback=my_stderr_callback,
    add_path_list=my_additional_path_list
)
# blocks until subprocess finishes

# you can get the buffered outputs as list of strings as well, besides the callbacks:
retcode, so, se = subpiper(
    cmd='echo magic',
    stdout_callback=my_stdout_callback,
    stderr_callback=my_stderr_callback,
    add_path_list=my_additional_path_list
)
print('STD out captured:', '\n'.join(so))
print('STD err captured:', '\n'.join(se))

Non-blocking mode

from subpiper import subpiper

def process_done(retcode: int):
    print(f'Subprocess finished with return code: {retcode}')

# call subpiper to take care everyting in non-blocking mode
subpiper(
    cmd='echo magic',
    stdout_callback=my_stdout_callback,
    stderr_callback=my_stderr_callback,
    add_path_list=my_additional_path_list,
    finished_callback=process_done
)
# do your other stuff
...
# when the subprocess finishes, your process_done callback will be invoked.

subpiper's People

Contributors

waszil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

subpiper's Issues

Support shell pipe

Hi waszil,

This is a really well-done python module. I really admire the way you handle stdout and stderr in real time with callback functions ;)

I'd like to ask if you had any plan to make it support shell piping, with no need to switch to "shell=True"?

Thanks!!

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.