diff --git a/gpt_engineer/core/default/disk_execution_env.py b/gpt_engineer/core/default/disk_execution_env.py index 8394de10a0..b13396f60d 100644 --- a/gpt_engineer/core/default/disk_execution_env.py +++ b/gpt_engineer/core/default/disk_execution_env.py @@ -22,6 +22,8 @@ - FilesDict: For handling collections of files. """ +import fcntl +import os import subprocess import time @@ -33,6 +35,17 @@ from gpt_engineer.core.files_dict import FilesDict +# Taken from https://gist.github.com/sebclaeys/1232088 +def non_block_read(output): + fd = output.fileno() + fl = fcntl.fcntl(fd, fcntl.F_GETFL) + fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) + try: + return output.readline() + except: + return "" + + class DiskExecutionEnv(BaseExecutionEnv): """ An execution environment that runs code on the local file system and captures @@ -88,8 +101,8 @@ def run(self, command: str, timeout: Optional[int] = None) -> Tuple[str, str, in while p.poll() is None: assert p.stdout is not None assert p.stderr is not None - stdout = p.stdout.readline() - stderr = p.stderr.readline() + stdout = non_block_read(p.stdout) + stderr = non_block_read(p.stderr) if stdout: print(stdout, end="") stdout_full += stdout