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 端执行结束后发布事件。

相关推荐
waterHBO1 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate4 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼4 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio6 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal7 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali8 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ8 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.8 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Trouvaille ~9 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算