我们为JupyterHub编写了一个自定义生成器以适应我们的用例 . 在同一个spawner中,我们对每个用户都有内存限制,并在 poll() 函数中进行检查,并在服务器上登录 .

我想要做的是当他接近时向客户端显示警报,例如大约90%的内存限制,另一个是100%,显示该过程被杀死 .

简单地说: I need to display an alert message on client browser window through the poll() function

它甚至可能吗?我可以使用Flask吗? (我在网上找到的资源是关于创建一个新服务器 . 在这种情况下,我们已经运行了一个jupyterhub服务器 . )

EDIT :这是我的客户端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秒运行一次 .