Hello,
I am working on a custom front end for an application and would like to use the ipython kernel. I am having trouble reliably receiving messages.
ipykernel==6.21.3
jupyter-client==8.0.3
With this script I can launch a kernel:
from ipykernel.kernelapp import IPKernelApp
app = IPKernelApp.instance()
app.initialize()
app.start()
Then I can communicate with the kernel with this script.
from pprint import pprint
import time
config = jupyter_client.find_connection_file('103940')
bkc = jupyter_client.BlockingKernelClient(connection_file=config)
bkc.load_connection_file()
print(bkc.comm_info())
msg_id = bkc.execute("print('hello nurse')",)
pprint(msg_id)
cnt = 0
while True:
if cnt >5:
break
print(cnt)
try:
io_msg = bkc.get_iopub_msg(timeout=1)
pprint(io_msg)
if io_msg['content']['execution_state'] == 'idle':
break
cnt += 1
except :
cnt +=1
Sometimes I don’t get the expected output. I’m missing messages. Like here.
ef94f968-2b3acf1fcb4e43c93b787c09_44128_0
'ef94f968-2b3acf1fcb4e43c93b787c09_44128_1'
0
{'buffers': [],
'content': {'execution_state': 'busy'},
'header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 54, 875647, tzinfo=tzutc()),
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_1',
'msg_type': 'status',
'session': 'd4d64e4e-b886ae43896eb1a5c6b20956',
'username': 'username',
'version': '5.3'},
'metadata': {},
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_1',
'msg_type': 'status',
'parent_header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 54, 859080, tzinfo=tzutc()),
'msg_id': 'ef94f968-2b3acf1fcb4e43c93b787c09_44128_0',
'msg_type': 'comm_info_request',
'session': 'ef94f968-2b3acf1fcb4e43c93b787c09',
'username': 'username',
'version': '5.3'}}
1
{'buffers': [],
'content': {'execution_state': 'idle'},
'header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 54, 876682, tzinfo=tzutc()),
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_3',
'msg_type': 'status',
'session': 'd4d64e4e-b886ae43896eb1a5c6b20956',
'username': 'username',
'version': '5.3'},
'metadata': {},
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_3',
'msg_type': 'status',
'parent_header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 54, 859080, tzinfo=tzutc()),
'msg_id': 'ef94f968-2b3acf1fcb4e43c93b787c09_44128_0',
'msg_type': 'comm_info_request',
'session': 'ef94f968-2b3acf1fcb4e43c93b787c09',
'username': 'username',
'version': '5.3'}}
But then other times I do get the output I expect. I have the code request and the results below.
8b262bf2-59000ed58224e27ff316cfe9_119476_0
'8b262bf2-59000ed58224e27ff316cfe9_119476_1'
0
{'buffers': [],
'content': {'execution_state': 'busy'},
'header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 57, 620336, tzinfo=tzutc()),
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_12',
'msg_type': 'status',
'session': 'd4d64e4e-b886ae43896eb1a5c6b20956',
'username': 'username',
'version': '5.3'},
'metadata': {},
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_12',
'msg_type': 'status',
'parent_header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 57, 609156, tzinfo=tzutc()),
'msg_id': '8b262bf2-59000ed58224e27ff316cfe9_119476_1',
'msg_type': 'execute_request',
'session': '8b262bf2-59000ed58224e27ff316cfe9',
'username': 'username',
'version': '5.3'}}
1
{'buffers': [],
'content': {'code': "print('hello nurse')", 'execution_count': 2},
'header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 57, 620964, tzinfo=tzutc()),
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_13',
'msg_type': 'execute_input',
'session': 'd4d64e4e-b886ae43896eb1a5c6b20956',
'username': 'username',
'version': '5.3'},
'metadata': {},
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_13',
'msg_type': 'execute_input',
'parent_header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 57, 609156, tzinfo=tzutc()),
'msg_id': '8b262bf2-59000ed58224e27ff316cfe9_119476_1',
'msg_type': 'execute_request',
'session': '8b262bf2-59000ed58224e27ff316cfe9',
'username': 'username',
'version': '5.3'}}
2
{'buffers': [],
'content': {'name': 'stdout', 'text': 'hello nurse\n'},
'header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 57, 622964, tzinfo=tzutc()),
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_14',
'msg_type': 'stream',
'session': 'd4d64e4e-b886ae43896eb1a5c6b20956',
'username': 'username',
'version': '5.3'},
'metadata': {},
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_14',
'msg_type': 'stream',
'parent_header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 57, 609156, tzinfo=tzutc()),
'msg_id': '8b262bf2-59000ed58224e27ff316cfe9_119476_1',
'msg_type': 'execute_request',
'session': '8b262bf2-59000ed58224e27ff316cfe9',
'username': 'username',
'version': '5.3'}}
3
{'buffers': [],
'content': {'execution_state': 'idle'},
'header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 57, 626271, tzinfo=tzutc()),
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_16',
'msg_type': 'status',
'session': 'd4d64e4e-b886ae43896eb1a5c6b20956',
'username': 'username',
'version': '5.3'},
'metadata': {},
'msg_id': 'd4d64e4e-b886ae43896eb1a5c6b20956_87728_16',
'msg_type': 'status',
'parent_header': {'date': datetime.datetime(2023, 3, 13, 22, 12, 57, 609156, tzinfo=tzutc()),
'msg_id': '8b262bf2-59000ed58224e27ff316cfe9_119476_1',
'msg_type': 'execute_request',
'session': '8b262bf2-59000ed58224e27ff316cfe9',
'username': 'username',
'version': '5.3'}}
I think I’m missing something here. Ideally I would like to use the jupyter_client
package only to send and receive messages. I have read through a lot of documentation but a lot of it seems out of date. It’s like messages are short lived and I’m missing them.
Any help or guidance would be appreciated. I’ve also tried using pyzmq
too but have not gotten anything working.