jupyter notebook WebSocket recv错误:套接字已关闭

yruzcnhs  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(167)

我尝试在jupyter notebook中创建session并使用jupyter notebook API执行代码,代码如下:

url = base + '/api/sessions'
    params = '{"path":\"%s\","type":"notebook","name":"","kernel":{"id":null,"name":"env37"}}' % file_name
    response = requests.post(url, headers=headers, data=params)
    session = json.loads(response.text)
    kernel = session["kernel"]

    # 讀取notebook檔案,並獲取每個Cell裡的Code
    url = base + '/api/contents' + notebook_path
    response = requests.get(url, headers=headers)
    file = json.loads(response.text)
    code = [c['source'] for c in file['content']['cells'] if len(c['source']) > 0]
    ws = create_connection("ws://127.0.0.1:8888/api/kernels/" + kernel["id"] + "/channels?session_id" + session["id"],
                           header=headers)
    for c in code:
        ws.send(json.dumps(send_execute_request(c)))

    # 我們只拿Code執行完的訊息結果,其他訊息將被忽略
    for i in range(0, len(code)):
        try:
            msg_type = ''
            while True:
                rsp = json.loads(ws.recv())
                msg_type = rsp["msg_type"]

字符串
但是在“ws.recv()"中,有一个错误:raise WebSocketConnectionClosedException(“socket is already closed.”)。
jupyter notebook cmd显示:

[I 18:04:05.904 NotebookApp] Kernel started: 275c3afd-cc10-4a69-8597-9f0d7f3e3a91, name: env37
[W 18:04:05.913 NotebookApp] Notebook example2.ipynb is not trusted
[W 18:04:05.917 NotebookApp] No session ID specified
[W 18:04:07.473 NotebookApp] No channel specified, assuming shell: {'header': {'msg_id': '9f4ce706980c11eebfe64ed5776c682d', 'username': 'test', 'session': '9f4cfa28980c11ee92b64ed5776c682d', 'data': '2023-12-11T18:04:07.471569', 'msg_type': 'execute_request', 'version': '5.0'}, 'parent_header': {'msg_id': '9f4ce706980c11eebfe64ed5776c682d', 'username': 'test', 'session': '9f4cfa28980c11ee92b64ed5776c682d', 'data': '2023-12-11T18:04:07.471569', 'msg_type': 'execute_request', 'version': '5.0'}, 'metadata': {}, 'content': {'code': 'from resync import resync', 'silent': False}}
[W 18:04:07.474 NotebookApp] No channel specified, assuming shell: {'header': {'msg_id': '9f4cfa29980c11eea45e4ed5776c682d', 'username': 'test', 'session': '9f4cfa2a980c11eebef84ed5776c682d', 'data': '2023-12-11T18:04:07.471569', 'msg_type': 'execute_request', 'version': '5.0'}, 'parent_header': {'msg_id': '9f4cfa29980c11eea45e4ed5776c682d', 'username': 'test', 'session': '9f4cfa2a980c11eebef84ed5776c682d', 'data': '2023-12-11T18:04:07.471569', 'msg_type': 'execute_request', 'version': '5.0'}, 'metadata': {}, 'content': {'code': '...', 'silent': False}}
[I 18:04:07.481 NotebookApp] Starting buffering for 275c3afd-cc10-4a69-8597-9f0d7f3e3a91:016c7619-9a09e6bff5dcdcab49729795


当我在jupyter notebook UI中关闭内核env37时,它显示:

[I 18:04:51.838 NotebookApp] Discarding 10 buffered messages for 275c3afd-cc10-4a69-8597-9f0d7f3e3a91:016c7619-9a09e6bff5dcdcab49729795
[I 18:04:51.838 NotebookApp] Kernel shutdown: 275c3afd-cc10-4a69-8597-9f0d7f3e3a91


我猜这是WebSocket在回复发送之前被关闭了,但是为什么以及如何解决呢?这个问题也发布在jupyter community forum

oknwwptz

oknwwptz1#

它通过使用create_connection和相同的参数重新连接到服务器来解决。注意,必须在消息的'data'部分指定时区。现在它可以接收服务器的回复。

相关问题