Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture output coming from C and C++ libraries #110

Open
dpiparo opened this issue Mar 12, 2016 · 13 comments
Open

Capture output coming from C and C++ libraries #110

dpiparo opened this issue Mar 12, 2016 · 13 comments

Comments

@dpiparo
Copy link

dpiparo commented Mar 12, 2016

Hi,

thanks for this excellent Kernel!
This is already a known behaviour: the output that is printed on screen by C/C++ libraries is not captured in the notebook: this can happen with ctypes or with Python bindings for C++ functions.
It would be terrific to have this output both captured and correctly interleaved with the output coming from Python and then printed "progressively" in the notebook.
For example, this loop could print every .5 secs a line from the Python and C world, interleaved:

from ctypes import *
libc = CDLL("libc.dylib")
import time
for i in xrange(10):
print "This is Python"
libc.printf("This is C\n")
time.sleep(.5)

At CERN we solved this issue for the ROOT Kernel in a custom way. For Python, we converged on a partial solution, which allows to correctly interleave the outputs but not to print them "live" in an asynchronous way.

I wonder if a general solution to this rather common issue, at least in HEP, could be envisaged, e.g. a "baby sitting process" capturing the output.

Cheers,
Danilo

@takluyver
Copy link
Member

(@dpiparo contacted me by email, and I suggested he open an issue so that the 'nanny process' idea we've been thinking of for a while gets back on our radar)

@dpiparo
Copy link
Author

dpiparo commented Mar 14, 2016

@takluyver , thanks for the clarification.

@minrk
Copy link
Member

minrk commented Mar 14, 2016

I made this package based on what we learned making the Cling kernel, so right now you can capture any C output in a notebook with:

with wurlitzer.sys_pipes():
    run_c_code()

@dpiparo
Copy link
Author

dpiparo commented Mar 14, 2016

I confirm Wurlitzer works great: thanks for sharing it! It successfully captures all output w/o freezing and is very easy to use. Technically one could think of having it enabled by default hooked on pre and post_execute.

The limitation I see, as Wurlitzer is today, is that the printing of the output can happen only at the end of the execution of the code/cell. In presence of long calculations the user may get the impression that the system is stuck even if the library underneath is trying to print some sort of progress indicator.

Another improvement that one might wish is the ability to properly "interleave" output from C(++) and Python by default.

@takluyver
Copy link
Member

Looking at the code, I think Wurlitzer should be capable of forwarding output 'live', without waiting to the end of the context. It's using a thread to pull from the read end of the pipe and forward it to Python sys.std*.

@minrk
Copy link
Member

minrk commented Mar 14, 2016

@dpiparo it should print outputs as they arrive, as long as you are using ipykernel >= 4.3.

The only impediment I see to integrating wurlitzer into IPython by default is how reliable it is on different platforms.

@minrk
Copy link
Member

minrk commented Mar 14, 2016

Since it is making ctypes calls, I could certainly imagine it causing crashes on weird systems. And I just assume it doesn't work on Windows.

@takluyver
Copy link
Member

I'm pretty sure there's no dup2 on Windows, so there's probably not a practical way to do this in process there.

@dpiparo
Copy link
Author

dpiparo commented Mar 14, 2016

On the other hand, if this would get integrated, on linux/mac would add many nice features while windows would stay as it is. I am not sure though how similar cases were treated in the past within the Jupyter project.

@minrk
Copy link
Member

minrk commented Mar 14, 2016

We have plenty of things that only work on certain platforms (subprocesses behave nicer on non-Windows due to pexpect, and web terminals only work on non-Windows, also due to using pexpect), so I don't see that as a big issue.

The only thing in between me and proposing that we use it on non-Windows is the stability question. I don't know enough to say if/when it won't work, even on posixy systems.

@takluyver
Copy link
Member

I don't think we should integrate that in the Python kernel - most stuff works well enough without it, and now there is an easy solution we can point people to when they do need it. I think the longer term answer to this is to get round to the nanny process we've talked about for a long time, because that lets us capture std streams at the OS level on all platforms without needing ctypes or anything.

Maybe this is something we can discuss at the dev meeting in a couple of weeks.

@minrk
Copy link
Member

minrk commented Mar 14, 2016

Sure.

@tomerk
Copy link

tomerk commented Jan 11, 2019

Hi, this is a pretty big issue for all Tensorflow users (because print operators run in the C++ internals)
tensorflow/community#14

Is it possible to revisit / reprioritize this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants