从spawner向用户显示警报消息

时间:2017-07-31 13:33:22

标签: javascript python flask tornado jupyterhub

我们为JupyterHub编写了一个自定义spawner,以适应我们的用例。在同一个spawner中,我们对每个用户都有内存限制,并在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秒运行一次。

0 个答案:

没有答案