Skip to content

Commit

Permalink
TEMP: Troubleshooting the incorrect writing -- making it operate in b…
Browse files Browse the repository at this point in the history
…inary stream mode
  • Loading branch information
yarikoptic committed May 7, 2024
1 parent 6cc6767 commit 9d64faf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
24 changes: 19 additions & 5 deletions src/duct.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
import sys
import threading
import time
from typing import Any, DefaultDict, Dict, List, Optional, TextIO, Tuple, Union
from typing import (
Any,
BinaryIO,
DefaultDict,
Dict,
List,
Optional,
TextIO,
Tuple,
Union,
)

__version__ = "0.0.1"
ENV_PREFIXES = ("PBS_", "SLURM_", "OSG")
Expand Down Expand Up @@ -217,16 +227,16 @@ class TeeStream:

write_fd: int
read_fd: int
file: TextIO
file: BinaryIO

CHUNK_SIZE = 1024

def __init__(self, file_path: str, stop_event: threading.Event):
self.stop_event = stop_event
self.file = open(file_path, "w")
self.file = open(file_path, "wb")
self.read_fd, self.write_fd = os.openpty()

def fileno(self):
def fileno(self) -> int:
"""Return the file descriptor to be used by subprocess as stdout/stderr."""
return self.write_fd

Expand All @@ -242,9 +252,13 @@ def _redirect_output(self):
chunk = stream.read(self.CHUNK_SIZE)
if not chunk:
break
# Yarik thinks this is a bad idea, because it might be
# a part of unicode and we need to write string here probably...
# If fails -- check/use solution from
# https://github.com/sassoftware/epdb/pull/13/files
sys.stdout.buffer.write(chunk)
sys.stdout.buffer.flush()
self.file.write(chunk.decode())
self.file.write(chunk)
self.file.flush()
except Exception as e:
print(f"DEBUG: Error in _redirect_output: {e}", file=sys.stderr)
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/test_tee_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
@pytest.mark.parametrize(
"file_path",
[
"test/e2e/ten_1",
"test/e2e/ten_2",
# "test/e2e/ten_3",
# "test/e2e/ten_1",
# "test/e2e/ten_2",
"test/e2e/ten_3",
# "test/e2e/ten_4",
# "test/e2e/ten_5",
# "test/e2e/ten_6",
Expand Down

0 comments on commit 9d64faf

Please sign in to comment.