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

Calling IPython.embed_kernel() outside the main thread fails silently #4032

Open
robo-corg opened this issue Aug 14, 2013 · 5 comments
Open
Milestone

Comments

@robo-corg
Copy link

Seems like this should either fail loudly or more preferably work :) Right now it prints out the instructions for connecting to it with --existing but doing so does not actually work.

I dug into why this was happening a little bit and it seems like the SIG_INT wrangling can't be done which is what is blowing up. Commenting out the spots where the kernel tries to do this seems to get embed_kernel actually working.

IPython.sys_info:

{'codename': 'An Afternoon Hack',
'commit_hash': '02da31c',
'commit_source': 'installation',
'default_encoding': 'UTF-8',
'ipython_path': '/usr/local/lib/python2.7/site-packages/IPython',
'ipython_version': '1.0.0',
'os_name': 'posix',
'platform': 'Darwin-12.4.0-x86_64-i386-64bit',
'sys_executable': '/usr/local/opt/python/bin/python2.7',
'sys_platform': 'darwin',
'sys_version': '2.7.5 (default, Aug 2 2013, 16:51:34) \n[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)]'}

@msabramo
Copy link
Contributor

msabramo commented Feb 6, 2015

Just ran into this as well. It fails silently for me.

@robo-hamburger: Can you post a bit more info on how you figured this out and how you made it work?

@robo-corg
Copy link
Author

I never really got cross thread kernel working. Instead a startup the kernel without the loop running to pump events and then just pump them each update in my game. This has the added benefit of the simulation being consistent but I suppose could get weird if you stopped running your own update loop.

First you need a way to startup a kernel with no loop see: https://gist.github.com/robo-hamburger/6237530

Then before your start your main loop:
self.pump_ipy_event = lambda: None

    self.console_locals = {
        # I just stuff random things in here so I don't need to import them
    }

    try:
        old_stdout = sys.stdout
        self.pump_ipy_event = embed_kernel_noloop(
            local_ns=self.console_locals)

        if old_stdout != sys.stdout:
            sys.stdout = Tee(old_stdout, sys.stdout)
    except Exception:
        logging.exception('Failed to initialize ipython')

Once per frame/update etc:
self.pump_ipy_event()

There maybe better APIs for this now but I haven't checked. I can clean this example up if you need additional help.

@msabramo
Copy link
Contributor

msabramo commented Feb 6, 2015

Interesting. What I ended up doing that is working okay for me so far is:

        with mock.patch('signal.signal'):
            IPython.embed_kernel(local_ns=namespace)

@robo-corg
Copy link
Author

I will have to remember that hack the next time I need this.

@msabramo
Copy link
Contributor

msabramo commented Feb 7, 2015

See https://github.com/msabramo/pyramid_ipython_kernel which applies this hack to Pyramid apps.

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

2 participants