Skip to content

Commit

Permalink
Merge pull request #385 from gdsfactory/extend_async_execution_output
Browse files Browse the repository at this point in the history
Support custom IO streams in `execute_and_stream_output`
  • Loading branch information
nikosavola authored Apr 23, 2024
2 parents f1a19ed + f9125d2 commit 7400a0e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions gplugins/common/utils/async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ async def execute_and_stream_output(
append: bool = False,
log_file_dir: Path | None = None,
log_file_str: str | None = None,
stream_stdout: io.TextIOWrapper | None = sys.stdout,
stream_stderr: io.TextIOWrapper | None = sys.stderr,
**kwargs,
) -> asyncio.subprocess.Process:
"""Run a command asynchronously and stream *stdout* and *stderr* to main and a log file
"""Run a command asynchronously and stream *stdout* and *stderr* to given IO and a log file
in ``log_file_dir / log_file_str``. Uses ``shell=True`` as default unlike ``subprocess.Popen``. Returns an asyncio process.
Args:
Expand All @@ -55,6 +57,8 @@ async def execute_and_stream_output(
append: Whether to use append to log file instead of writing.
log_file_dir: Directory for log files.
log_file_str: Log file name. Will be expanded to ``f'{log_file_str}_out.log'`` and ``f'{log_file_str}_err.log'``.
stream_stdout: Stream to write stdout to. Defaults to ``sys.stdout``.
stream_stderr: Stream to write stderr to. Defaults to ``sys.stderr``.
``*args`` and ``**kwargs`` are passed to :func:`~create_subprocess_shell` or :func:`create_subprocess_exec`,
which in turn passes them to :class:`subprocess.Popen`.
Expand All @@ -78,15 +82,15 @@ async def execute_and_stream_output(
asyncio.create_task(
handle_return(
proc.stdout,
out_stream=sys.stdout,
out_stream=stream_stdout,
log_file=log_file_dir / f"{log_file_str}_out.log",
append=append,
)
)
asyncio.create_task(
handle_return(
proc.stderr,
out_stream=sys.stderr,
out_stream=stream_stderr,
log_file=log_file_dir / f"{log_file_str}_err.log",
append=append,
)
Expand Down

0 comments on commit 7400a0e

Please sign in to comment.