An Engineering Notebook post. Feel free to ignore.
The following sketches how to create a non-blocking client within Leo. At
present, it is exactly equivalent to the blocking code, but imo it shows to
define classes that will become non-blocking. Few actual overrides will
likely be needed.
Notice the single import, and the resulting fully explicit base classes.
(Zen of python #2).
Also notice that start_non_blocking_kernel handles caching of
previously-created kernels and managers.
import jupyter_client
class NonBlockingChannel (jupyter_client.blocking.channels.ZMQSocketChannel
):
pass
class NonBlockingKernelClient (jupyter_client.blocking.client.
BlockingKernelClient):
pass
class NonBlockingManager (jupyter_client.manager.KernelManager):
client_class = NonBlockingKernelClient
client_factory = NonBlockingKernelClient
def getIn(r, path):
for p in path:
r = r.get(p, {})
return r
def start_non_blocking_kernel(startup_timeout=60, kernel_name='python', **
kwargs):
'''
Like manager.start_new_kernel, but instantiates a NonBlockingManager.
This method also caches the manager and client for later use.
'''
d = g.app.permanentScriptDict
kc = d.get('kc')
km = d.get('km')
if kc and km:
return km, kc
km = NonBlockingManager(kernel_name=kernel_name)
km.start_kernel(**kwargs)
kc = km.client()
kc.start_channels()
try:
kc.wait_for_ready(timeout=startup_timeout)
except RuntimeError:
kc.stop_channels()
km.shutdown_kernel()
raise
d['kc'] = kc
d['km'] = km
return km, kc
def main(code):
km, kc = start_non_blocking_kernel()
kc.execute(code=code, user_expressions={'a':'a'})
msg = kc.get_shell_msg()
print(getIn(msg, ['content', 'user_expressions', 'a', 'data',
'text/plain']))
code = '''\
import math
a = math.sin(1)
'''
main(code)
All comments welcome.
The next step will be to override methods so that checking for results
happens asynchronously.
Edward
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.