You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在一个Python文件中并行运行两个位于不同网络驱动器的Python文件

结论

这个需求完全可以实现。你已经将本地系统作为网络驱动器挂载到云端机器,云端可直接访问存放在本地的sub_file_local.py,直接通过并行执行逻辑调用两个脚本即可。

前置检查
  • 确认云端机器访问挂载的网络驱动器路径稳定,运行main_file.py的用户对sub_file_local.py有可读可执行权限
  • 确认sub_file_local.py所需的第三方依赖已在云端的Python环境中安装完成
  • 确认sub_file_local.py没有调用仅本地环境才能访问的专属资源(如本地未挂载的目录、本地专属硬件等)
实现示例

用Python标准库concurrent.futures和subprocess即可实现并行调用,无需安装额外依赖:

import concurrent.futures
import subprocess
import sys

def run_script(script_path):
    # 调用Python脚本,捕获输出和返回码
    result = subprocess.run(
        [sys.executable, script_path],
        capture_output=True,
        text=True
    )
    return (script_path, result.returncode, result.stdout, result.stderr)

if __name__ == "__main__":
    # 替换为实际路径:云端sub脚本路径、挂载的本地网络驱动器下的sub脚本路径
    SCRIPT_PATHS = [
        "/cloud_path/sub_file_cloud.py",
        "Z:/mount_local_path/sub_file_local.py" # Windows挂载一般为盘符,Linux挂载一般为/mnt下的路径
    ]

    # 并行执行两个脚本
    with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
        results = executor.map(run_script, SCRIPT_PATHS)

    # 输出执行结果
    for path, code, stdout, stderr in results:
        print(f"脚本 {path} 执行结束,返回码:{code}")
        if stdout:
            print(f"标准输出:{stdout}")
        if stderr:
            print(f"标准错误:{stderr}")
注意事项
  • 两个脚本的执行计算资源全部占用云端机器的CPU、内存,网络驱动器仅负责读取sub_file_local.py的文件内容
  • 网络驱动器的访问延迟会影响sub_file_local.py的启动速度,对脚本本身的执行速度无影响
  • 如果两个脚本存在共享资源竞争逻辑,需要自行加锁处理,避免并行运行产生数据冲突
  • 如果sub_file_local.py必须在本地环境运行,可以在本地启动简易HTTP接口,云端main_file.py通过网络请求触发执行,当前挂载方案可满足大部分无本地专属资源依赖的场景

内容的提问来源于stack exchange,提问作者Sudarshanajay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 11:15:06