I am running multiple notebooks at the same time, often in different browsers, sometimes on different remote clients. till now, when I close the tab corresponding to a running notebook, it warns that the corresponding run will be stopped.
My question: How do I make a jupyter notebook resume it's run even if the page is closed ?
From what I understand, the client-server architecture could make that possible, but that there may be issues with multiple concurrent runs...
PS: I also asked the question on SO and will make sure to make this issue updated if I get solutions.
Anything already running in the notebook will keep running, and the kernel it started for that will stay running - so it won't lose your variables. However, any output produced while the notebook isn't open in a browser tab is lost; there isn't an easy way to change this until we have the notebook server able to track the document state, which has been on the plan for ages.
@takluyver -- I have verified that the notebook keeps running in the background with:
from time import sleep
file = open("jupyter_tab_close_test", "w")
file.close()
i = 0
while True:
print("{}\n".format(i))
i +=1
with open("jupyter_tab_close_test", "a") as f:
f.write("{}\n".format(i))
sleep(1)
When I tail -f the file the output keeps coming after I close the tab.
I had an idea for how to at least keep output to stdout, but it does not work, and I don't understand why.
I would expect to see the output pick up where it left off being written to the console. I don't see that.
@takluyver Is the problem handled? If not, maybe we can redirect the output into a file so that it is available in a different console. But how could it be easy to achieve (write as little code as possible)?
There is a kind of interim fix in place: when the browser disconnects, the server should now buffer output messages until the browser reconnects, and then send them through.
Great news!
There is a kind of interim fix in place: when the browser disconnects, the server should now buffer output messages until the browser reconnects, and then send them through.
How does that work in practice. How do I disconnect my browser from the session other than closing the tab?
Closing the tab is the only explicit way to do it, as far as I'm aware. It may also happen if network issues mean the server can't send output to your browser.
In either case, some outputs may be lost if they're sent before it detects that the websocket is disconnected.
@takluyver alright thanks :). I'll give it a try.
Otherwise someone said over here that you can just run some other cell after a disconnect and reconnect to get all the output.
Or is this what you've been referring to?
Maybe. That person seems to be using Google Colab, though, so it might be a bit different.
Simple fix could be instead of just using the built-in print, redefine it and save it to a temporary string. Then you can access the notebook again, examine that string instead. Of course we have to be care the string doesn't get ridiculously large.
just log output to a file so you can see it later - e.g. something like
"""
message writer. log to file
"""
def message( msg ):
data = str(datetime.datetime.now()) + " " + msg
print(data)
with open(status_file, 'a') as sfile:
if msg[-1] != '\n':
msg += '\n'
sfile.write(data)
Most helpful comment
Anything already running in the notebook will keep running, and the kernel it started for that will stay running - so it won't lose your variables. However, any output produced while the notebook isn't open in a browser tab is lost; there isn't an easy way to change this until we have the notebook server able to track the document state, which has been on the plan for ages.