Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve robustness of subprocess text streaming #6445

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/prefect/utilities/processutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import anyio.abc
from anyio.streams.text import TextReceiveStream, TextSendStream

TextSink = Union[TextIO, TextSendStream]
TextSink = Union[anyio.AsyncFile, TextIO, TextSendStream]


@asynccontextmanager
Expand Down Expand Up @@ -101,9 +101,17 @@ async def consume_process_output(


async def stream_text(source: TextReceiveStream, sink: Optional[TextSink]):
if isinstance(sink, TextIOBase):
# Convert the blocking sink to an async-compatible object
sink = anyio.wrap_file(sink)

async for item in source:
if isinstance(sink, TextSendStream):
await sink.send(item)
elif isinstance(sink, TextIOBase):
sink.write(item)
sink.flush()
elif isinstance(sink, anyio.AsyncFile):
await sink.write(item)
await sink.flush()
elif sink is None:
pass # Consume the item but perform no action
else:
raise TypeError(f"Unsupported sink type {type(sink).__name__}")