Skip to main content

Thread safe stdio proxy

Project description

stdio_proxy

PyPI PyPI Supported Python Versions GitHub license GitHub Actions (Tests)

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
Hello from th1:1
===Done===
Redirected Stdout:
Hello from th2:0
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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

stdio_proxy-0.1.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

stdio_proxy-0.1.2-py2-none-any.whl (6.0 kB view details)

Uploaded Python 2

File details

Details for the file stdio_proxy-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: stdio_proxy-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for stdio_proxy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9f5cb8bb545fd5dbdd50c2842e691dd6a6874947c5e9aa2c00f7d3bbf4a15a8e
MD5 d95d4f536c451c30555cdc28085d422c
BLAKE2b-256 551566befbe59e5d109ab866c4990972e342b77f03e687393799056beef30edb

See more details on using hashes here.

File details

Details for the file stdio_proxy-0.1.2-py2-none-any.whl.

File metadata

  • Download URL: stdio_proxy-0.1.2-py2-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 2
  • Uploaded using 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

File hashes

Hashes for stdio_proxy-0.1.2-py2-none-any.whl
Algorithm Hash digest
SHA256 8bdccc85905c11b5e68eb4ab89cd197a31af518dabb75e00175950484abd751d
MD5 950affce8ad025f7f65671d752bba2fc
BLAKE2b-256 554696a1c6c173c67eafbdd5073ff7b594d37153e449ac259176f7464831e94a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page