是否有一种方法以编程方式防止谷歌Colab超时断开?

笔记本电脑自动断开连接的情况如下:

谷歌Colab笔记本的空闲超时为90分钟,绝对超时为12小时。这意味着,如果用户没有与他的谷歌Colab笔记本进行交互超过90分钟,其实例将自动终止。此外,Colab实例的最大生存期为12小时。

自然,我们希望自动地从实例中挤出最大的值,而不必不断地手动与它交互。这里我将假设常见的系统需求:

Ubuntu 18.04 LTS (Bionic Beaver), Windows 10或Mac操作系统 对于基于linux的系统,使用流行的桌面环境,如GNOME 3或Unity Firefox或Chromium浏览器

我应该在这里指出,这样的行为并不违反谷歌Colab的使用条款,尽管根据他们的常见问题解答,这是不鼓励的(简而言之:从道德上讲,如果你真的不需要它,那么用完所有的gpu是不可以的)。


我目前的解决方案非常愚蠢:

首先,我把屏保关掉,这样我的屏幕就一直开着。 我有一个Arduino板,所以我只是把它变成了一个橡胶鸭子USB设备,让它在我睡觉的时候模拟原始的用户交互(只是因为我手头有其他用例)。

有没有更好的办法?


当前回答

我一直在寻找一个解决方案,直到我发现了一个Python 3脚本,它随机地来回移动鼠标并单击,总是在同一个地方。但这足以骗过协作实验室,让它以为我在笔记本上活动,它不会断开连接。

import numpy as np
import time
import mouse
import threading

def move_mouse():
    while True:
        random_row = np.random.random_sample()*100
        random_col = np.random.random_sample()*10
        random_time = np.random.random_sample()*np.random.random_sample() * 100
        mouse.wheel(1000)
        mouse.wheel(-1000)
        mouse.move(random_row, random_col, absolute=False, duration=0.2)
        mouse.move(-random_row, -random_col, absolute=False, duration = 0.2)
        mouse.LEFT
        time.sleep(random_time)


x = threading.Thread(target=move_mouse)
x.start()

sudo -H pip3 install <package_name>

你只需要用sudo(在你的本地机器上)运行它(因为它控制鼠标),它就能工作,让你充分利用协作实验室的12小时会话。

学分:对于那些使用Colab (Pro):防止会话由于不活动而断开

其他回答

对我来说,以下是一些例子:

document.querySelector(“#连接”).click()或 document.querySelector(“colab-toolbar-button #连接”).click()或 document.querySelector(“colab-connect-button”).click ()

我们失误了。

我必须调整它们以适应以下情况:

版本1:

function ClickConnect(){
  console.log("Connnect Clicked - Start"); 
  document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click();
  console.log("Connnect Clicked - End"); 
};
setInterval(ClickConnect, 60000)

版本2: 如果你想停止这个函数,下面是新代码:

var startClickConnect = function startClickConnect(){
    var clickConnect = function clickConnect(){
        console.log("Connnect Clicked - Start");
        document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click();
        console.log("Connnect Clicked - End"); 
    };

    var intervalId = setInterval(clickConnect, 60000);

    var stopClickConnectHandler = function stopClickConnect() {
        console.log("Connnect Clicked Stopped - Start");
        clearInterval(intervalId);
        console.log("Connnect Clicked Stopped - End");
    };

    return stopClickConnectHandler;
};

var stopClickConnect = startClickConnect();

为了停止,调用:

stopClickConnect();

你也可以使用Python来按方向键。我也在下面的代码中添加了一点随机性。

from pyautogui import press, typewrite, hotkey
import time
from random import shuffle

array = ["left", "right", "up", "down"]

while True:
    shuffle(array)
    time.sleep(10)
    press(array[0])
    press(array[1])
    press(array[2])
    press(array[3])

投票最多的答案当然适合我,但它使管理会话窗口一次又一次地弹出。 我已经解决了这个问题,使用浏览器控制台自动单击刷新按钮,如下所示

function ClickRefresh(){
    console.log("Clicked on refresh button"); 
    document.querySelector("paper-icon-button").click()
}
setInterval(ClickRefresh, 60000)

请随意在这个gist https://gist.github.com/Subangkar/fd1ef276fd40dc374a7c80acc247613e上贡献更多的片段

我不相信JavaScript解决方案还能继续工作。我在我的笔记本里写了:

    from IPython.display import display, HTML
    js = ('<script>function ConnectButton(){ '
           'console.log("Connect pushed"); '
           'document.querySelector("#connect").click()} '
           'setInterval(ConnectButton,3000);</script>')
    display(HTML(js))

当你第一次执行Run all(在JavaScript或Python代码开始之前)时,控制台显示:

Connected to
wss://colab.research.google.com/api/kernels/0e1ce105-0127-4758-90e48cf801ce01a3/channels?session_id=5d8...

然而,每次JavaScript运行时,你都会看到console.log部分,但点击部分只是给出:

Connect pushed

Uncaught TypeError: Cannot read property 'click' of null
 at ConnectButton (<anonymous>:1:92)

其他人建议按钮名称已更改为# coab -connect-button,但这也会产生相同的错误。

After the runtime is started, the button is changed to show RAM/DISK, and a drop down is presented. Clicking on the drop down creates a new <DIV class=goog menu...> that was not shown in the DOM previously, with 2 options "Connect to hosted runtime" and "Connect to local runtime". If the console window is open and showing elements, you can see this DIV appear when you click the dropdown element. Simply moving the mouse focus between the two options in the new window that appears adds additional elements to the DOM, as soon as the mouse looses focus, they are removed from the DOM completely, even without clicking.

GNU Colab允许您在协作实验室实例之上运行标准的持久桌面环境。

事实上,它包含了一种机制,不让机器空转死亡。

这是一个视频演示。