我们为 JupyterHub 编写了一个自定义生成器以适应我们的用例。在同一个生成器中,我们对每个用户都有内存限制,这在poll()
函数中进行检查,并登录到服务器上。
我想要做的是在客户接近时向客户显示警报,比如大约 90% 的内存限制,另一个是 100%,显示进程正在被杀死。
简而言之:我需要通过该功能在客户端浏览器窗口上显示警报消息poll()
甚至可能吗?我可以使用 Flask 吗?(我在网上找到的资源是关于创建一个新服务器的。在这种情况下,我们已经有一个 jupyterhub 服务器在运行。)
编辑:这是我的客户端 Javascript:
<script type="text/javascript">
// Client-side Javascript in the HTML
var targetContainer = document.getElementById("this-div");
var eventSource = new EventSource("/stream");
eventSource.onmessage = function(e) {
targetContainer.innerHTML = e.data;
};
</script>
这是poll()
功能:
@gen.coroutine
def poll(self):
"""Poll the spawned process to see if it is still running.
If the process is still running, we return None. If it is not running,
we return the exit code of the process if we have access to it, or 0 otherwise.
"""
self.log.debug("Polling " + self.user.name + "...")
# peh = ProxyErrorHandler()
# peh.get(503)
self.stream()
这是stream()
功能:
app = Flask(__name__)
@app.route("/stream")
def stream(self):
def eventStream():
while True:
yield "Hello World"
return Response(eventStream(), mimetype="text/event-stream")
Javascript 会抛出一个404 Not Found
错误,这是有道理的,因为 poll() 函数仅每 30 秒运行一次。