mirror of
https://github.com/liamcottle/reticulum-meshchat.git
synced 2026-04-27 16:10:32 +00:00
force python to immediatly flush stdout and stderr streams so other programs such as nodejs can use immediately
This commit is contained in:
parent
4165d5291d
commit
9d1c0bce1a
1 changed files with 30 additions and 0 deletions
30
src/__init__.py
Normal file
30
src/__init__.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import sys
|
||||
|
||||
# NOTE: this class is required to be able to use print/log commands and have them flush to stdout and stderr immediately
|
||||
# without wrapper stdout and stderr, when using `childProcess.stdout.on('data', ...)` in NodeJS script, we never get
|
||||
# any events fired until the process exists. However, force flushing the streams does first the callbacks in NodeJS.
|
||||
|
||||
|
||||
# this class forces stream writes to be flushed immediately
|
||||
class ImmediateFlushingStreamWrapper:
|
||||
|
||||
def __init__(self, stream):
|
||||
self.stream = stream
|
||||
|
||||
# force write to flush immediately
|
||||
def write(self, data):
|
||||
self.stream.write(data)
|
||||
self.stream.flush()
|
||||
|
||||
# force writelines to flush immediately
|
||||
def writelines(self, lines):
|
||||
self.stream.writelines(lines)
|
||||
self.stream.flush()
|
||||
|
||||
def __getattr__(self, attr):
|
||||
return getattr(self.stream, attr)
|
||||
|
||||
|
||||
# wrap stdout and stderr with our custom wrapper
|
||||
sys.stdout = ImmediateFlushingStreamWrapper(sys.stdout)
|
||||
sys.stderr = ImmediateFlushingStreamWrapper(sys.stderr)
|
||||
Loading…
Add table
Add a link
Reference in a new issue