jupyter kernelgateway对外暴露服务

本文主要介绍 jupyter kernelgateway 在工作中的运用

基本介绍

Jupyter Kernel Gateway 是一个 Web 服务器,提供对 Jupyter 内核的无头访问。应用程序通过 REST 调用和 Websockets 远程与内核通信。

安装

shell 复制代码
pip install jupyter_kernel_gateway

运行

shell 复制代码
jupyter kernelgateway

定义一个./my_example.ipynb,使用 jupyter kernelgateway 对外暴露服务

python 复制代码
# GET /hello/world

import json
import requests
import numpy as np

req = json.loads(REQUEST)
res = dict(data=np.random.randn(5, 4).tolist(), request=req)
print(json.dumps(res))
bash 复制代码
jupyter kernelgateway --KernelGatewayApp.api=kernel_gateway.notebook_http --KernelGatewayApp.seed_uri=./my_example.ipynb --port=10100
[KernelGatewayApp] Kernel started: 12ac2daa-c62a-47e4-964a-336734557656
[KernelGatewayApp] Registering resource: /hello/world, methods: (['GET'])
[KernelGatewayApp] Registering resource: /_api/spec/swagger.json, methods: (GET)
[KernelGatewayApp] Jupyter Kernel Gateway at http://127.0.0.1:10100

GET 调用

定义一个带有两个路径参数 firstName 和 lastName 的接口

python 复制代码
# GET /hello/:firstName/:lastName

req = json.loads(REQUEST)
data = req['path']
logging.info("name:%s" % (data['firstName']), extra = extra)
logging.info("name:%s" % (data['lastName']), extra = extra)

POST 调用

python 复制代码
# POST /consistency_check/hive_vs_hive

req = json.loads(REQUEST)
data = req['body']
logging.info("请求参数:%s" % json.dumps(data), extra = extra)

可以看到 GET 和 POST ,取值的不同,GET 是从 REQUEST 对象的 path中取值,POST 是从 REQUEST 对象的 body中取值。

工作使用

两个数仓之间的数据比对、以及 hive 和 mysql 之间的数据导入导出,都是用 pyspark 实现的。

在 java 端会调用 python 服务,此时就是使用 jupyter kernelgateway 来对外暴露 python 服务的。

那么 java 和 python 之间是怎么通信的呢?

其实是通过 redis 的发布订阅模式,java 端去订阅channel,python 端执行结束后发布事件。

相关推荐
最强小杰16 分钟前
Claude Opus 4.8 调用报 529 怎么办?并发限制触发过载——退避策略与降级方案完整指南
运维·网络·python·ai
databook30 分钟前
MinMax vs Standard vs Robust:三种特征缩放怎么选
python·机器学习·数据分析
在世修行32 分钟前
智学文枢:实体模型层
python·orm·sqlalchemy
张张123y1 小时前
# 后端第七课:FastAPI 实战入门(用刚学的 Python 写第一个 API 接口)
开发语言·python·mysql·api·fastapi
CCblandy1 小时前
一致性哈希到底省了多少搬迁?本机实测 3 轮:取模 6→7 搬 85.8%,V=128 只搬 14.3%,虚拟节点太少会被倾斜骗
后端·python·云原生·性能优化·架构
Yan-英杰1 小时前
【论文精读】 基于 TensorFlow 对卷积神经网络的优化研究:异构式 CPU+GPU 协同计算 + ReLU-Swish 激活函数
python·卷积神经网络
ydjs678899-bot1 小时前
如何在Java中打印pdf静默打印和打印对话框选择
java·python·pdf
把我格子衫拿来2 小时前
误差预算怎么做?从雅可比到蒙特卡洛的三维定位不确定度分析
python·蒙特卡洛·三维定位·误差分析·不确定度
文章永久免费只为良心2 小时前
Ctfshow_Web应用安全与防护_第二章_无字母数字命令执行
前端·python·安全·网络安全·渗透测试·ctf·红队渗透测试
2601_962885723 小时前
如何用 Python 获取上证指数、沪深300、中证500 等指数行情?
开发语言·python