Closed IPython Notebook that was running code

不羁岁月 提交于 2019-12-23 09:36:29

问题


How it works? I got some code running in an IPython Notebook. Some iterative work.

Accidentally I closed the browser with the running Notebook, but going back to the IPython Dashboard I see that this particular Notebook hasn't Shutdown, so if I open the Notebook again I see the [*] in front of my code that it was executing.

I even can hear my PC still running the code, but it doesn't return me any new output of the print statements.

Can I wait and eventually continue with the output, or my PC will still running my code, but it won't be accessible anymore?


回答1:


When you start up ipython, it is essentially creating a web server that is running on a separate process. The code itself is running on the web server, or kernel. The web browser is simply one of several front-ends that can view and edit the code on the kernel.

This design allows ipython to separate the evaluation of code from the viewing and editing of code -- for example, I could access the same kernel via the web interface (ipython notebook), the console (ipython console), or the qt console interface (ipython qtconsole).

Your PC will continue to run the code, though I believe that the output requested by one frontend will not show on any other frontends using the same kernel (I'm not 100% certain about this though).

You can find more information here.




回答2:


I am struggling with this issue as well for some time now. The kernel keeps running your job on the server, but there is no way to see the console output after closing the browser.

My workaround was to write all my logs to a file, so that when my browser closes (indeed when a lot of logs come through browser it hangs up too) I can see the kernel job process by opening the log file (the log file can be open using Jupyter too).

#!/usr/bin/python
import time
import datetime
import logging

logger = logging.getLogger()

def setup_file_logger(log_file):
    hdlr = logging.FileHandler(log_file)
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr) 
    logger.setLevel(logging.INFO)

def log(message):
    #outputs to Jupyter console
    print('{} {}'.format(datetime.datetime.now(), message))
    #outputs to file
    logger.info(message)

setup_file_logger('out.log')

for i in range(10000):
    log('Doing hard work here i=' + str(i))
    log('Taking a nap now...')
    time.sleep(1000)


来源:https://stackoverflow.com/questions/20699961/closed-ipython-notebook-that-was-running-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!