I have just discovered that blocking client can function also in 
non-blocking mode. Just adding keyword argument block=False (which default 
value is True in BlockingKernelClient) in get_..._msg calls is enough to 
exhibit non-blocking functionality.  See the implementation of blocking 
channels in jupyter_client.blocking.channels.

Here is a script that shows non-blockiness:
import jupyter_client
import jupyter_client.manager
import time
try:
    from queue import Empty
except ImportError:
    from Queue import Empty
code = '''import math
import time
time.sleep(0.5)
a = math.sin(1)'''
def getIn(r, pth):
    for p in pth:
        r = r.get(p, {})
    return r
def getClient():
    if c.user_dict.get('kmkc') is None:
        km, kc = jupyter_client.manager.start_new_kernel()
        c.user_dict['kmkc'] = km, kc
    else:
        km,kc = c.user_dict['kmkc']
    return kc
def execute_one_cell(code):
    kc = getClient()
    msgid = kc.execute(code=code, user_expressions={'a':'a'})
    waitTo = time.time() + 3 # we will wait no longer than 3 sec
    n = 0
    while True:
        try:
            msg = kc.get_shell_msg(block=False)
            if getIn(msg, ['parent_header', 'msg_id']) != msgid:
                # we are expecting reply to our own request not
                # somebody's else msg
                continue
            g.es('Empty received %d times'%n)
            return getIn(msg, ['content', 'user_expressions', 'a', 'data', 
'text/plain'])
        except Empty:
            n += 1 # shows that our communication with kc is not blocking
            time.sleep(0.1)
            if time.time() > waitTo:
                g.es('timeout')
                return None

g.es('reslult is:', execute_one_cell(code))


I have added in calculation code sleep of 0.5 sec to show that 
get_shell_msg is not blocking.
Vitalije

-- 
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.

Reply via email to