Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
stdio_proxy is a thread-safe library for Python 2.7 and Python 3.5+ that can temporarily redirect stdio to another objects.
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.
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))
Hello from th1:0
Hello from th1:1
===Done===
Redirected Stdout:
Hello from th2:0
Hello from th2:1
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()
$ pip install stdio-proxy
stdin
buf = io.BytesIO(b"foo\n")
with stdio_proxy.redirect_stdin(buf):
print("Read: {}".format(sys.stdin.readline()))
stdout
to a bufferbuf = io.BytesIO()
with stdio_proxy.redirect_stdout(buf):
print("foo")
print("Redirected: {}".format(buf.getvalue()))
stderr
to a bufferbuf = io.BytesIO()
with stdio_proxy.redirect_stderr(buf):
sys.stderr.write("foo\n")
print("Redirected: {}".format(buf.getvalue()))
MIT License
FAQs
Thread safe stdio proxy
We found that stdio-proxy demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.