从产生的地方向用户显示警报信息

2024-04-26 12:42:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我们已经为JupyterHub编写了一个定制产卵器,以适合我们的用例。在同一个生成程序中,我们对每个用户都有内存限制,这是在poll()函数中检查的,并登录到服务器上。你知道吗

我想做的是在客户端接近时向其显示一个警报,例如90%的内存限制,另一个为100%,显示进程正在被终止。你知道吗

简单地说:我需要通过poll()函数在客户端浏览器窗口上显示一条警报消息

有可能吗?我能用烧瓶吗?(我在网上找到的资源是关于创建新服务器的。在本例中,我们已经运行了一个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秒运行一次。你知道吗


Tags: the函数self服务器客户端streamreturnis