Google Colab - How to 'restart runtime' using python code or command line interface?

后端 未结 3 1148
滥情空心
滥情空心 2020-12-31 15:09

Can someone suggest me programatically \'restart runtime\'? Any programmatically restarting option is fine, python or CLI (Command Line Interface), without using GUI.

相关标签:
3条回答
  • 2020-12-31 15:41

    You could simply use:

    exit()
    

    This will restart the runtime without deleting files. The runtime will automatically start. And if you press "run all" the run is not interrupted and works till the end.

    0 讨论(0)
  • 2020-12-31 15:43

    To add to Bob's answer - once you kill yourself, there's no going back. So you won't be able to programmatically resume the execution after the self-destruct call.

    But you can make it work without changing code - i.e. there's a limited number of reasons to restart (e.g. installing modules, especially if they were imported, switching Tensorflow versions etc.), and in your final run of the notebook, they shouldn't be necessary.

    Here's a complete example of installing Detectron2 on top of a notebook about object detection:

    %%time
    # deps installation
    try:
      import detectron2
    except ImportError:
      !git clone https://github.com/facebookresearch/detectron2 detectron2_repo
      !pip install -e detectron2_repo
      print('Stopping RUNTIME! Please run again.')
      import os
      os.kill(os.getpid(), 9)
    
    0 讨论(0)
  • 2020-12-31 16:04

    Run a cell with the following code snippet:

    import os
    os.kill(os.getpid(), 9)
    

    That will kill the current Python runtime process, which will be automatically restarted by the manager.

    0 讨论(0)
提交回复
热议问题