Google Colab session timeout

Viewed 66394

In the FAQs it is mentioned that

Virtual machines are recycled when idle for a while, and have a maximum lifetime enforced by the system.

Are the maximum lifetime and idle times fixed or variable? Is there any way to predict them?

4 Answers

It's 90 minutes if you close the browser. 12 hours if you keep the browser open. Additionally, if you close your browser with a code cell is running, if that same cell has not finished, when you reopen the browser it will still be running (the current executing cell keeps running even after browser is closed)

PROBLEM: I have to training my model for hours but the google colab keeps disconnecting after 30 mins automatically if I do not click frequently, leading to loss of all data.

SOLUTION:

Steps:

  1. Open the inspector view by typing Ctrl+ Shift + i and then clicking on console tab at top.
  2. Paste the below code snippet at bottom of console and hit enter
function ClickConnect(){
console.log("Working"); 
document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click();
}
setInterval(ClickConnect,60000)
  1. Believe me, that's all folks. Above code would keep on clicking the page and prevent it from disconnecting.

Below is the image showing console view of above steps:-

enter image description here

enter image description here

Alternatively you can also try below snippet:

interval = setInterval(function() { 
    console.log("working")
    var selector = "#top-toolbar > colab-connect-button"
    document.querySelector(selector).shadowRoot.querySelector("#connect").click()
    setTimeout(function() {
            document.querySelector(selector).shadowRoot.querySelector("#connect").click()
    }, 1000)
}, 60*1000)

Improving to @Ashish Anand's answer

Use this code when you want to start:

function ClickConnect(){
console.log("Working"); 
document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click();
}
var clicker = setInterval(ClickConnect,60000);

And the following code when you need to stop:

clearInterval(clicker);

Another way to overcome the session timeout is to run an autoclick script in python (especially if you intend not to use your computer while running your code in colab)

Here is the code : (Be sure to pip install pynput before)

import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode


delay = 20 #this is the delay of the autoclick (20 seconds here)
button = Button.left
start_stop_key = KeyCode(char='s')
exit_key = KeyCode(char='e')


class ClickMouse(threading.Thread):
    def __init__(self, delay, button):
        super(ClickMouse, self).__init__()
        self.delay = delay
        self.button = button
        self.running = False
        self.program_running = True

    def start_clicking(self):
        self.running = True

    def stop_clicking(self):
        self.running = False

    def exit(self):
        self.stop_clicking()
        self.program_running = False

    def run(self):
        while self.program_running:
            while self.running:
                mouse.click(self.button)
                time.sleep(self.delay)
            time.sleep(0.1)


mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()


def on_press(key):
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
    elif key == exit_key:
        click_thread.exit()
        listener.stop()


with Listener(on_press=on_press) as listener:
    listener.join()

Run this script on a commandline window, and then press the key "s" to start autoclicking and "e" for exit, than leave the mouse pointer on a code cell (normally it will click after a certain delay).

Related