17

Is it possible to run some long tasks in IPython Notebook, close browser and then after some time open it again and reveal results of these tasks?

Say, task like this:

def f():
    import time
    time.sleep(100)
    with open("result.txt", "w") as fh:
        fh.write("Done.")

If I run task normally, and close browser before completion, as I open it back, I see no 'result.txt'.

If I run it using %px magic or parallel execution - again no result if I close browser before completion.

Any extensions or hacks available? Or am I missing something?

UPDATE 1:

Although there is background jobs control support in IPython, background jobs become stale after I disconnect browser. The only thing I could come up with is issuing %connect_info before closing browser, and then connect from screen terminal using

ipython console --existing <ID>.json

and run my jobs from there.

UPDATE 2:

Even more helpful hack is combination of ipython console and job control. I.e. I open console and attach to the same session while starting background job in browser and then I'm free to close it until job is finished.

UPDATE 3:

it seems to work since version iPython 1.0dev without any hacks. you just run what you want, close browser and it still runs.

dimo414
  • 47,227
  • 18
  • 148
  • 244
Anaderi
  • 775
  • 5
  • 8

2 Answers2

3

I normally use the linux command screen, which opens a different shell that can run in the background.

For example, when you first enter your shell you type 'screen'. It then gives you a new terminal. Start ipython there with the above command. Once it is running, on your keyboard press CTRL+A CTRL+D. The program then starts running in the background and you can close the terminal. If you want to get back to it, type 'screen -r' in the command line.

Stephan
  • 125
  • 5
  • Stephan, maybe I was not clear, but I'm asking about iPython notebook - which is a browser-based console. And I've found no way to use console-based browsers to access iPython from under screen. – Anaderi Apr 23 '13 at 11:39
1

I don't know if this is new in IPython 1.0, but in my IPython notebook, if I call your function f(), and close the browser, the kernel is running in the background and after a while (I changed to time.sleep(10)) I see the results.txt file generated. I don't think the kernel, which you usually start in a console, stops when you close the browser window. Let me know if I am mistaken.

joon
  • 3,899
  • 1
  • 40
  • 53