太多的sse连接挂起了网页

时间:2021-04-26 06:46:42

What limits the number of SSE(server sent event) connections?

什么限制了SSE(服务器发送事件)连接的数量?

I have been working on a project using django/gunicorn/django-sse.

我一直在使用django / gunicorn / django-sse开展一个项目。

My project works great when i limit the number of sse connections to the page (5 works 6 hangs), this isnt a huge problem cause i use pagination so can limit the number per page. but i would prefer to be able to have as many as i like.

当我限制页面的sse连接数量时,我的项目很有效(5个工作6个挂起),这不是一个很大的问题,因为我使用分页因此可以限制每页的数量。但我宁愿能拥有我喜欢的那么多。

My question is: is it the number of connections that is slowing it down, or is it the amount of data being transfered?

我的问题是:连接的数量是减慢速度,还是传输的数据量?

the first problem i think i could fix by making them share a connection but the second would probably limit me a bit more.

第一个问题,我认为我可以通过让他们共享一个连接来解决,但第二个可能会限制我更多。

Any ideas which it may be?

它可能是什么想法?

EDIT:

client side JS SSE code:

客户端JS SSE代码:

function event(url, resource_name, yes, no, audio_in, audio_out, current_draw){
    /**
     * Listens for events posted by the server
     * 
     * Useful site for understanding Server Sent Events:
     *    http://www.w3.org/TR/eventsource/
     */
    var source = new EventSource(url);
    source.addEventListener("message", function(e) {
        resetTime(resource_name);
        data = updateStatus(e.data, yes, no, audio_in, audio_out, current_draw);
        document.getElementById(resource_name+"-in").src = data.audio_in_src
        document.getElementById(resource_name+"-in").alt = data.audio_in_alt
        document.getElementById(resource_name+"-out").src = data.audio_out_src
        document.getElementById(resource_name+"-out").alt = data.audio_out_alt
        document.getElementById(resource_name+"-current").innerHTML = data.current_draw + " A"
    });
}

in views.py

class ServerSentEvent(RedisQueueView):

    def get_redis_channel(self):
        """
        Overrides the RedisQueueView method to select the channel to listen to
        """
        return self.kwargs["resource_name"]

in urls.py

urlpatterns = patterns('',
                       url(r'^$',
                           views.Resources_page.as_view(),
                           name='resources_page'),
                       url(r'^(?P<resource_name>\w+)/$',
                           views.StatusPage.as_view(),
                           name='status_page'),
                       url(r'^(?P<resource_name>\w+)/sse/$',
                           views.ServerSentEvent.as_view(),
                           name='sse'),)

3 个解决方案

#1


0  

If you're using the sync worker for gunicorn (the default), then you can only have as many concurrent connections to your server as you have worker processes.

如果您正在使用同步工作程序用于gunicorn(默认设置),那么您只能拥有与工作进程一样多的并发连接到服务器。

The sync worker is designed for CPU-bound tasks, hence the recommendation to use 2N + 1 workers (where N is the number of cores available). If your SSE endpoint is the logical equivalent of this...

同步工作程序专为CPU绑定任务而设计,因此建议使用2N + 1个工作程序(其中N是可用的核心数)。如果你的SSE端点是这个的逻辑等价物......

while True:
    msg = "foo"
    yield msg
    sleep(1)

...then you have an I/O-bound view. No matter how much CPU time you throw at that block of code, it's designed to never end. If you use the django_sse project, then this is almost exactly what your SSE view is doing.

...然后你有一个I / O绑定视图。无论你在那段代码中浪费了多少CPU时间,它都是永远不会结束的。如果您使用django_sse项目,那么这几乎就是您的SSE视图正在执行的操作。

The solution is to use an asynchronous worker class for gunicorn. Install gevent and pass --worker-class=gevent option to gunicorn and you're on your way to an asynchronous utopia.

解决方案是使用异步工作者类作为gunicorn。安装gevent并将--worker-class = gevent选项传递给gunicorn,你正在前往异步乌托邦。

#2


0  

I had the exact same problem. I was sure I was using gevent as the worker, but I only got around 6 connections.

我有同样的问题。我确信我使用gevent作为工作者,但我只有6个连接。

The solution was stupid. This was a browser limitation. I am writing it down here for the next person to stumble on this to see..

解决方案很愚蠢。这是一个浏览器限制。我在这里写下来为下一个人偶然发现这个看到..

In firefox, there is a parameter in about:config that is named network.http.max-persistent-connections-per-server that controls this. So the solution in my case was increasing that number from 6 (default), or using several browsers..

在firefox中,about:config中有一个参数,名为network.http.max-persistent-connections-per-server,用于控制它。所以我的案例中的解决方案是从6(默认)或使用多个浏览器增加该数字。

#3


0  

The browser usually limit the connections to same server, you can check that from the configuration of your browser, e.g. in Firefox, you can check on page "about:config", where you can see such key:value { network.http.speculative-parallel-limit 6 }, of course, you can change the number to more and test.

浏览器通常会限制与同一服务器的连接,您可以从浏览器的配置中检查,例如在Firefox中,您可以在页面上查看“about:config”,在这里您可以看到这样的键:value {network.http.speculative-parallel-limit 6},当然,您可以将数量更改为更多并进行测试。

So this is not the problem on server side.

所以这不是服务器端的问题。

#1


0  

If you're using the sync worker for gunicorn (the default), then you can only have as many concurrent connections to your server as you have worker processes.

如果您正在使用同步工作程序用于gunicorn(默认设置),那么您只能拥有与工作进程一样多的并发连接到服务器。

The sync worker is designed for CPU-bound tasks, hence the recommendation to use 2N + 1 workers (where N is the number of cores available). If your SSE endpoint is the logical equivalent of this...

同步工作程序专为CPU绑定任务而设计,因此建议使用2N + 1个工作程序(其中N是可用的核心数)。如果你的SSE端点是这个的逻辑等价物......

while True:
    msg = "foo"
    yield msg
    sleep(1)

...then you have an I/O-bound view. No matter how much CPU time you throw at that block of code, it's designed to never end. If you use the django_sse project, then this is almost exactly what your SSE view is doing.

...然后你有一个I / O绑定视图。无论你在那段代码中浪费了多少CPU时间,它都是永远不会结束的。如果您使用django_sse项目,那么这几乎就是您的SSE视图正在执行的操作。

The solution is to use an asynchronous worker class for gunicorn. Install gevent and pass --worker-class=gevent option to gunicorn and you're on your way to an asynchronous utopia.

解决方案是使用异步工作者类作为gunicorn。安装gevent并将--worker-class = gevent选项传递给gunicorn,你正在前往异步乌托邦。

#2


0  

I had the exact same problem. I was sure I was using gevent as the worker, but I only got around 6 connections.

我有同样的问题。我确信我使用gevent作为工作者,但我只有6个连接。

The solution was stupid. This was a browser limitation. I am writing it down here for the next person to stumble on this to see..

解决方案很愚蠢。这是一个浏览器限制。我在这里写下来为下一个人偶然发现这个看到..

In firefox, there is a parameter in about:config that is named network.http.max-persistent-connections-per-server that controls this. So the solution in my case was increasing that number from 6 (default), or using several browsers..

在firefox中,about:config中有一个参数,名为network.http.max-persistent-connections-per-server,用于控制它。所以我的案例中的解决方案是从6(默认)或使用多个浏览器增加该数字。

#3


0  

The browser usually limit the connections to same server, you can check that from the configuration of your browser, e.g. in Firefox, you can check on page "about:config", where you can see such key:value { network.http.speculative-parallel-limit 6 }, of course, you can change the number to more and test.

浏览器通常会限制与同一服务器的连接,您可以从浏览器的配置中检查,例如在Firefox中,您可以在页面上查看“about:config”,在这里您可以看到这样的键:value {network.http.speculative-parallel-limit 6},当然,您可以将数量更改为更多并进行测试。

So this is not the problem on server side.

所以这不是服务器端的问题。