Last active
June 26, 2019 18:00
-
-
Save ktlim/4e9deae888b150bb6e17efb32c1e46c3 to your computer and use it in GitHub Desktop.
Log to Jupyter cell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import threading | |
class LogRedirect: | |
def __init__(self, fd=1, dest=sys.stderr, encoding="utf-8", errors="strict"): | |
# Save original handle so we can restore it later. | |
self.saved_handle = os.dup(fd) | |
self.saved_fd = fd | |
self.saved_dest = dest | |
# Redirect `fd` to the write end of the pipe. | |
pipe_read, pipe_write = os.pipe() | |
os.dup2(pipe_write, fd) | |
os.close(pipe_write) | |
# This thread reads from the read end of the pipe. | |
def consumer_thread(f, data): | |
while True: | |
buf = os.read(f, 1024) | |
if not buf: | |
break | |
data.write(buf.decode(encoding, errors)) | |
os.close(f) | |
return | |
# Spawn consumer thread, and give it a mutable `data` item to | |
# store the redirected output. | |
self.thread = threading.Thread(target=consumer_thread, args=(pipe_read, dest)) | |
self.thread.start() | |
def finish(self): | |
# Cleanup: flush streams, restore `fd` | |
self.saved_dest.flush() | |
os.dup2(self.saved_handle, self.saved_fd) | |
os.close(self.saved_handle) | |
self.thread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment