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

Google Colab错误107:Transport endpoint is not connected解决咨询

解决Google Colab长时间运行后Drive挂载断开(错误107)的方案

核心原因

Colab的Drive挂载会话会因长时间无交互或底层连接超时自动断开,即便是订阅用户也存在连接时长限制,重复执行drive.mount()可能因旧挂载点未清理而无效。

具体解决方案

1. 挂载时强制卸载旧挂载点

每次重新挂载前先清理旧连接,避免残留问题:

from google.colab import drive
import os

# 强制卸载已挂载的Drive(如果存在)
if os.path.exists('/content/drive'):
    !fusermount -u /content/drive
# 重新挂载
drive.mount('/content/drive', force_remount=True)

将这段代码放在写入Drive的关键节点(比如每小时一次,或每次写入前),确保挂载状态有效。

2. 用本地存储中转数据

长时间处理时,先把中间文件存在Colab本地/content/目录,最后一次性写入Drive,减少Drive连接时长:

# 中间结果写入本地
merged_data.to_csv('/content/temp_merged.csv', index=False)
# 最终复制到Drive
!cp /content/temp_merged.csv /content/drive/MyDrive/your_target_path/

本地存储速度更快,还能避免频繁读写Drive引发的连接不稳定。

3. 直接使用Drive API操作

跳过挂载环节,用Drive API直接读写文件,绕开FUSE挂载的连接限制:

from google.colab import auth
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

# 完成认证
auth.authenticate_user()
drive_service = build('drive', 'v3')

# 上传本地文件到指定Drive文件夹
file_metadata = {'name': 'final_merged.csv', 'parents': ['your_folder_id']}
media = MediaFileUpload('/content/temp_merged.csv', mimetype='text/csv')
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()

需替换your_folder_id为目标Drive文件夹的ID(从文件夹URL中提取)。

4. 增加会话保活机制

Colab后台会话可能因长时间无交互被回收,添加简单脚本保持活跃:

import IPython
from google.colab import output

# 每10分钟执行一次空操作,维持会话
keep_alive_script = """
function keepAlive() {
  console.log('保持会话活跃');
  setTimeout(keepAlive, 600000); // 10分钟触发一次
}
keepAlive();
"""
output.register_callback('keepAlive', keep_alive_script)
IPython.display.Javascript(keep_alive_script)

该脚本在浏览器后台运行,防止会话因无交互断开。

5. 拆分长任务

将4-5小时的任务拆分为多个短任务,每处理一部分数据就保存一次,重启会话后继续处理,避免单次会话时长超限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 16:02:35