Python tornado太多打开文件Ssl

时间:2017-08-30 19:46:36

标签: python django ssl tornado

如果我的龙卷风服务器上有很多连接,我会在日志上发生错误

<script>
            var ctx = document.getElementById('directtraffic').getContext('2d');
            var directtraffic = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: ["2017-05-01","2017-05-02","2017-05-03","2017-05-04","2017-05-05","2017-05-06","2017-05-07","2017-05-08","2017-05-09","2017-05-10","2017-05-11","2017-05-12","2017-05-13","2017-05-14","2017-05-15","2017-05-16","2017-05-17","2017-05-18","2017-05-19","2017-05-20","2017-05-21","2017-05-22","2017-05-23","2017-05-24","2017-05-25","2017-05-26","2017-05-27","2017-05-28","2017-05-29","2017-05-30","2017-05-31"],
                    datasets: [{
            label: 'Sessions (01/05/2017 - 31/05/2017)',
            data: ["169","278","287","223","252","129","97","246","266","285","262","203","107","121","319","261","336","291","227","110","94","237","263","259","239","172","79","77","111","257","233"],
            responsive: true,
            tooltips: {
                mode: 'index',
                intersect: false,
            },
            legend: {
                display: false,
            },
            hover: {
                mode: 'nearest',
                intersect: true
            },
            backgroundColor: '#5cbae6',
            borderColor: '#5cbae6',
            fill: false
        },{
            label: 'Sessions (01/04/2017 - 30/04/2017)',
            data: ["103","100","261","280","258","250","197","70","94","248","206","239","198","113","113","108","99","248","245","250","225","107","86","244","245","268","299","223","98","119"],
            responsive: true,
            tooltips: {
                mode: 'index',
                intersect: false,
            },
            legend: {
                display: false,
            },
            hover: {
                mode: 'nearest',
                intersect: true
            },
            backgroundColor: '#b6d957',
            borderColor: '#b6d957',
            fill: false
        }]
                },
                options: {
                    animation: false,
                    scales: {
                        xAxes: [{
                            gridLines: {
                                display:false
                            }
                        }],
                        yAxes: [{
                            gridLines: {
                                display:false
                            }  
                        }]
                    }
                }
            });
            </script>

并断开我的客户端。 Tornado在ewery connect上打开ssl证书文件?

龙卷风应用

Exception in callback (<socket._socketobject object at 0x7f0b9053e3d0>, <function null_wrapper at 0x7f0b9054c140>)
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/netutil.py", line 276, in accept_handler
    callback(connection, address)
  File "/usr/local/lib/python2.7/dist-packages/tornado/tcpserver.py", line 264, in _handle_connection
  File "/usr/local/lib/python2.7/dist-packages/tornado/netutil.py", line 517, in ssl_wrap_socket
    context = ssl_options_to_context(ssl_options)
  File "/usr/local/lib/python2.7/dist-packages/tornado/netutil.py", line 494, in ssl_options_to_context
    context.load_cert_chain(ssl_options['certfile'], ssl_options.get('keyfile', None))
IOError: [Errno 24] Too many open files

从django命令启动龙卷风服务器

class VastWebSocket(tornado.websocket.WebSocketHandler):
connections = set()
current_connect = 0
current_user = 0
status_play_vast = False

def open(self):
    c = Connection()
    c.connection = self

VastWebSocket.connections.add(c)
self.current_connect = c

def on_message(self, msg):

data = json.loads(msg)

app_log.info("on message = " + msg)

if not 'status' in data:
  return

if data["status"] == "start_vast":
  VastWebSocket.status_play_vast = True

if data["status"] == "end_vast":
  VastWebSocket.status_play_vast = False

app_log.info("status_play_vast = " + str(VastWebSocket.status_play_vast))

if data["status"] == "get_status_vast":
  self.current_connect.connection.write_message({"status": VastWebSocket.status_play_vast})
  return

for conn in self.connections:
        conn.connection.write_message(msg)


def on_close(self):
if self.current_connect <> 0:
  VastWebSocket.connections.remove(self.current_connect)


def check_origin(self, origin):
return True

为什么他打开很多文件,在我看来需要在启动服务器上打开ssl和所有。 Stackoverflow会询问更多信息,但在顶部,所有信息都需要。

1 个答案:

答案 0 :(得分:1)

Your code seems to run multiple process or thread that all accessing to the ssl keyfile directly. Linux default import pandas as pd import sqlite3 import math con = sqlite3.connect(r'C:\Python34\factbook.db') facts = pd.read_sql_query('select * from facts;', con) facts.dropna() facts = facts[facts['area_land']!=0][:] facts = facts[facts['population']!=0][:] def pop_in_50(name): pop=facts[facts['name']==name]['population'] prec = facts[facts['name']==name]['population_growth'] new_pop = pop*math.e**(35*(prec/100)) return new_pop facts['pop_in_2050'] = facts['name'].apply(pop_in_50) is quick low and this error then occurs.

You can check the current setting with:

<div id="clock">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

#clock div {
  width: 50px;
  height: 50px;
  background-color: blue;
  display: inline-block;
}

The quick and dirty solution is to increase this value:

<div>

Even ulimit is accepted with the $ ulimit -a option.

Note: You can permanently set the value the app user $ ulimit -n <new_value> file.

In both case you'll need to log out then log in for the change to take effect.

But modifying this value is a bit dirty because it's a global value for a given user environment.

The harder but cleaner solution is to find a way to load the content of the file in memory when your app load and make the loaded value/variable accessible for all processes.

相关问题