传输 (Transport) 插件#

The term transport in AiiDA refers to a class that the engine uses to perform operations on local or remote machines where its CalcJob are submitted. The base class Transport defines an interface for these operations, such as copying files and executing commands. A transport plugin is a class that implements this base class for a specific connection method. The aiida-core package ships with two transport plugins: the LocalTransport and SshTransport classes. The local transport can be used to connect with the localhost and makes use only of some standard python modules like os and shutil. The ssh transport, which can be used for machines that can be connected to over ssh, is simply a wrapper around the library paramiko that is installed as a required dependency of aiida-core.

开发插件#

实际上,用户几乎从未直接使用过传输类。它主要由 engine 使用,engine 使用传输插件连接到它所管理的计算作业正在运行的计算机。engine 必须能够始终使用相同的方法,无论需要使用哪种传输方式连接到相关计算机。

通用传输类包含一组最基本的方法,为了与其他插件完全兼容,实现必须支持这些方法。如果不支持,就会出现 NotImplementedError ,从而中断使用传输插件的计算或其他管理工作。

至于插件的一般功能, __init__() 方法仅用于初始化类实例,并不实际打开传输通道。连接必须由 __enter__() 方法打开(由 __exit__() 关闭)。通过 __enter__() 方法,您可以使用 with 语句(参见 python docs )来使用传输类,方法类似于下面的内容:

with TransportPlugin() as transport:
    transport.some_method()

例如,为了确保这一点,本地插件使用了一个隐藏的布尔变量 _is_open ,该变量在调用 __enter__()__exit__() 方法时被设置。而 ssh 逻辑则由属性 sftp 提供。

其他需要注意的函数是复制函数,其术语如下:

  1. put :从本地源到远程目的地

  2. get :从远程源到本地目的地

  3. copy :从远程源向远程目标复制文件

请注意,这些函数必须同时接受文件和文件夹,并且在内部会退回到 putfileputtree 等函数。

最后一个需要注意的函数是 exec_command_wait() ,它与 subprocess python 模块类似。该函数允许将字符串作为远程命令执行,因此如果编写不慎,可能会产生令人讨厌的效果。

警告

请确保为 bash 转义任何字符串!

下载 此模板 作为实现新传输插件的起点。该模板包含接口和所有需要实现的方法,其中的文档说明可与 Sphinx 文档一起使用。

备注

要通知 AiiDA 您的新传输插件,您必须在 aiida.transports entry point 组中注册一个 entry point。请访问 AiiDA registry ,查看如何实现的示例。

登录 shells#

基本传输类 aiida.transports.transport.Transport 定义了 use_login_shell 选项。当设置为 True 时,所有通过传输执行的命令都将使用 bash-l/--login 选项。根据 bash 的手册,这将指示 bash 加载登录名 shell,意思是

当以交互式登录 shell 或带有 --login 选项的非交互式 shell 调用 bash 时,如果文件 /etc/profile 存在,它会首先读取并执行该文件中的命令。读取该文件后,它会依次查找 ~/.bash_profile~/.bash_login~/.profile ,并读取和执行第一个存在且可读取的文件中的命令。

默认情况下, use_login_shell 设置为 True ,因为它可以确保 AiiDA 通过传输执行的命令与用户手动登录并执行命令时看到的 shell 环境相同。然而,在某些情况下,登录脚本可能不是 AiiDA 在目标计算机上正确运行代码的必要条件。同时,登录脚本可能有不可忽略的运行时间,因此会大大降低 AiiDA 执行命令的速度。在这种情况下,将 use_login_shell 设置为 False 可能会有用。