rossant / ipycache

Defines a %%cache cell magic in the IPython notebook to cache results of long-lasting computations in a persistent pickle file
BSD 3-Clause "New" or "Revised" License
138 stars 35 forks source link

output in cached cells is delayed #16

Closed ihrke closed 9 years ago

ihrke commented 10 years ago

Hi,

when running a cell under the %%cache magic, the output is delayed until the evaluation has finished. I.e., in the following code, 1 shows up after 10 secs.

%%cache 'test.pkl' var
import time
for i in range(10):
    time.sleep(1)
    print i
var=2

I usually insert progress-bars or other on-the-fly output to long computations so that I'm able to estimate how long they are going to take. Is it possible to enable dynamic output in ipycache? I'm not sure what would be the best way to proceed since I'm not very familiar with ipython's internals.

ihrke commented 10 years ago

Sorry for the fuzz. Turns out, it IS an issue of ipycache, after all. The same problem was also caused by a different plugin in my setup which is why I thought that it was not caused by ipycache...

rossant commented 10 years ago

I'm not sure why this bug is happening actually... I don't see why the standard output would be captured. That requires some testing...

ihrke commented 10 years ago

Maybe you just have to add a sys.stdout.flush() someplace? Unfortunately, I don't know how ipython handles this, so I'm not a big help.

ihrke commented 10 years ago

I checked out ipycache.py and the problem seems to be because you capture the cells' output using the capture_output() handler:

with capture_output() as io:
   try:
      ip_run_cell(cell)
   except:
      # Display input/output.
      io()
      return
# code, code
io()

which is used to inject the captured output in case the cell is not evaluated.

So I guess, to make the output show up in real-time, the capture-output handler would have to be modified?

ihrke commented 9 years ago

I experimented a bit with the capture_output() handler and found a solution that resolves the "bug" (see ihrke/ipycache@8d05a45b620ead800a6acf9379b8051213d4e545). The branch is here: https://github.com/ihrke/ipycache/tree/capture_and_print However, the solution is quite ugly as it requires redefining ipython's capture_output() handler to use a custom derivation of StringIO which writes out its input and stores it internally.

Do you have a better idea how that could be handled?

rossant commented 9 years ago

I'm wondering if it would be possible to just not use IPython's code but an entirely custom context manager? something like http://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call

I initially used whatever IPython did because that was just quicker, but if it's too limited we can use something else.

ihrke commented 9 years ago

Actually, that's kind of what I did (by copying and adapting the code from ipython). I thought that you used ipython's capture handler for some deeper reason but I guess we are alright with my solution, then. We need all the ipython features anyway because we need to handle output graphics etc. Is it ok if I merge my code into the main repo?

rossant commented 9 years ago

If that works sure, we can always refine later if needed. Thanks.

ktran9891 commented 6 years ago

This is still not working for me for some reason. Any suggestions?