编码.docker命令行代码执行器
Docker命令行代码执行器
class DockerCommandLineCodeExecutor(CodeExecutor)
__init__
def __init__(image: str = "python:3-slim",
container_name: Optional[str] = None,
timeout: int = 60,
work_dir: Union[Path, str] = Path("."),
bind_dir: Optional[Union[Path, str]] = None,
auto_remove: bool = True,
stop_container: bool = True,
execution_policies: Optional[Dict[str, bool]] = None)
(实验性)一种通过Docker容器中的命令行环境执行代码的代码执行器类。
执行器首先将每个代码块保存在工作目录中的一个文件中,然后在容器中执行该代码文件。执行器按照接收顺序执行代码块。目前,执行器仅支持Python和shell脚本。对于Python代码,请使用"python"作为代码块的语言。对于shell脚本,请使用"bash"、"shell"或"sh"作为代码块的语言。
参数:
image
类型, 可选 - 用于代码执行的Docker镜像。 默认为 "python:3-slim"。container_name
Optional[str], optional - Docker 容器的名称 如果为 None,将自动生成一个名称。默认为 None。timeout
int, 可选 - 代码执行的超时时间。默认为60。work_dir
Union[Path, str], optional - 代码执行的工作目录。默认为 Path(".")。bind_dir
Union[Path, str], optional - 将被绑定到代码执行器容器的目录。适用于你想要从容器内部生成容器的情况。默认为 work_dir。auto_remove
bool, 可选 - 如果为 true,将在 Docker 容器停止时自动移除它。默认为 True。stop_container
bool, 可选 - 如果为 true,当调用 stop 时,当上下文管理器退出时,或者当 Python 进程退出时,将自动停止容器。默认值为 True。
引发:
ValueError
- 参数错误,或容器启动失败时。
超时
@property
def timeout() -> int
(实验性的)代码执行的超时时间。
工作目录
@property
def work_dir() -> Path
(实验性)代码执行的工作目录。
绑定目录
@property
def bind_dir() -> Path
(实验性)代码执行容器的绑定目录。
code_extractor
@property
def code_extractor() -> CodeExtractor
(实验性)导出一个可以被代理使用的代码提取器。
执行代码块
def execute_code_blocks(code_blocks: List[CodeBlock]) -> CommandLineCodeResult
(实验性)执行代码块并返回结果。
参数:
code_blocks
List[CodeBlock] - 要执行的代码块。
返回:
CommandlineCodeResult
- 代码执行的结果。
重启
def restart() -> None
(实验性)重启代码执行器。
停止
def stop() -> None
(实验性)停止代码执行器。