Skip to main content

stdio_proxy

stdio_proxy is a thread-safe library for Python 2.7 and Python 3.5+ that can temporarily redirect stdio to another objects.

Background

Python 3.5+ has redirect_stdout and redirect_stderr in contextlib, that are utility functions for temporarily redirecting sys.stdout and sys.stderr to another file-like objects, respectively. But those functions have the global side effect on sys.stdout and sys.stderr. That means we cannot use those functions in most threaded applications.

  • Python code
import contextlib
import io
import time
from concurrent.futures import ThreadPoolExecutor

def run(value):
    for i in range(2):
        print("Hello from {}:{}".format(value, i))
        time.sleep(1)

def run_hook(value):
    buf = io.StringIO()
    with contextlib.redirect_stdout(buf):
        run(value)
    return buf.getvalue()

with ThreadPoolExecutor() as executor:
    f1 = executor.submit(run, "th1")
    f2 = executor.submit(run_hook, "th2")
    f1.result()
    result = f2.result()
    print("===Done===")
    print("Redirected Stdout:\n{}".format(result))
  • What we want
Hello from th1:0
===Done===
Redirected Stdout:
Hello from th2:0
Hello from th1:1
Hello from th2:1
  • Example of actual output
Hello from th1:0
===Done===
Redirected Stdout:
Hello from th2:0
Hello from th1:1
Hello from th2:1

This library aims to redirect those stdio correctly in threaded applications as well. By just replacing run_hook function with the following code, the result would be exactly the same as "what we want" :)

def run_hook(value):
    buf = io.BytesIO()
    with stdio_proxy.redirect_stdout(buf):
        run(value)
    return buf.getvalue()

Install

$ pip install stdio-proxy

Usage

  • Redirect a buffer to stdin
buf = io.BytesIO(b"foo\n")
with stdio_proxy.redirect_stdin(buf):
    print("Read: {}".format(sys.stdin.readline()))
  • Redirect stdout to a buffer
buf = io.BytesIO()
with stdio_proxy.redirect_stdout(buf):
    print("foo")
print("Redirected: {}".format(buf.getvalue()))
  • Redirect stderr to a buffer
buf = io.BytesIO()
with stdio_proxy.redirect_stderr(buf):
    sys.stderr.write("foo\n")
print("Redirected: {}".format(buf.getvalue()))

License

MIT License

Metadata

Release files for stdio-proxy 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for stdio-proxy 0.1.0
File Interpreter ABI Platform
stdio_proxy-0.1.0-py3-none-any.whl Python 3 none any Details
stdio_proxy-0.1.0-py2-none-any.whl Python 2 none any Details

Total release size: 11.2 kB

Release files / stdio_proxy-0.1.0-py3-none-any.whl

Download URL stdio_proxy-0.1.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
624d88de2a84b0f387ade27c68daa1306228fa1392f085f2941642173caf5024
BLAKE2b-256 checksum
How to use checksums
471f70759b38ab32810c4ab3da11a3dfcfa0f6fc5680094b7c15d14640514947
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.6.9

Release files / stdio_proxy-0.1.0-py2-none-any.whl

Download URL stdio_proxy-0.1.0-py2-none-any.whl
Size 5.6 kB
Tags Python 2
SHA-256 checksum
How to use checksums
ec94a21e76898b69320d39d6d027dacfda83c9392021297a39d5825bee9dd35b
BLAKE2b-256 checksum
How to use checksums
1d5c483650576ae84b8d0b13a4c32bc22ef0b0778911af70f2d4270ea53d96eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.6.9

Release history Release notifications | RSS feed

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page