我的python脚本开始工作后服务器崩溃

时间:2016-04-09 09:05:56

标签: python mysql sockets python-3.x mysql-python

我有一个python脚本,它监听tcp端口,并在收到数据和一个小字符串处理后保存mysql数据库中的数据:

脚本运行并开始工作真的很好但是过了一会儿,起初我得到了mysql连接错误然后服务器挂了并且在崩溃后下降!!!

我的代码是:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import socket
import os
import threading
import mysql.connector
import sys
import signal
from datetime import datetime, date,time
import time as timehelper
#//******defines******\\#
HOST = '127.0.0.1'
PORT = 8889

Dconn = mysql.connector.connect(user='root', password='peiman64',host='127.0.0.1',database='prisoner',charset='utf8')
print('databased stablished.')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(50)
print ('Socket listening started')
def processString(stri,addr):
    arr = stri.split('#')
    arr = arr[2].split(',')
    print(arr)
    if len(arr)<7 :
        sys.exit()
    imei=arr[0]
    password=arr[1]
    indate=arr[2]
    intime=arr[3]
    gislat=arr[4]
    gislong=arr[5]
    speed=arr[6]
    btrylvl=arr[13]
    cursor = Dconn.cursor(buffered=True)
    devCountresult = "select COUNT(*) as num,dvid from devices where code="+imei
    cursor.execute(devCountresult)
    resC,dId = cursor.fetchone()
    if resC ==0 :
        cursor.execute("INSERT INTO devices (code) VALUES ("+imei+")")
        devId = cursor.lastrowid
    else:
        devId = dId
    d = date(int(indate[4:8]), int(indate[2:4]), int(indate[0:2]))
    t = time(int(intime[0:2]), int(intime[2:4]), int(intime[4:6]))
    fTime = datetime.combine(d, t)
    fTime =int(datetime.timestamp(fTime))
    cursor.execute("INSERT INTO raw (device, longt, lat,battery, recv_time,dev_time) VALUES ('"+format(devId)+"','"+gislong+"','"+gislat+"','"+format(btrylvl)+"','"+format(timehelper.time())+"','"+format(fTime)+"' )")
    insertedId = cursor.lastrowid
    serverResult = "select * FROM servers where status='active';"
    cursor.execute(serverResult)
    serer = cursor.fetchall()
    for ser in serer :
        cursor.execute("INSERT INTO `row_sent` (`signal_id`, `server_id`) VALUES ('"+format(insertedId)+"', '"+format(ser[0])+"');")
    print('aaa')
    Dconn.commit()
    print(insertedId)
    if insertedId>0:
        return 1
    else:
        return 0
#//******socket treadingg******\\#
def clientthread(conn,addr):
    while True:
        data = conn.recv(1024)
        adata = data.strip().decode('utf-8')
        if len(adata) > 0:
            processString(adata,addr)
            reply ="#AL#1###\r\n"
            conn.send(bytes(reply, 'UTF-8'))
#//******----------------******\\#
while True:
    conn, addr = s.accept()
    print ('Connected with ' + addr[0] + ':' + str(addr[1]))
    t = threading.Thread(target=clientthread, args=((conn),(addr),))
    t.start()
s.close()

服务器很干净,没有任何其他软件!

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

这种服务器崩溃发生了传递最大打开文件限制

对于管理有关最大可用文件打开过程的内存,Os有一个软硬限制

和端口正在克隆为虚拟文件缓冲区

你可以检查限制,如:

[root@server~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 62797
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 62797
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

以及

[root@server~]# ulimit -aH
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 62797
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 4096
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) unlimited
cpu time               (seconds, -t) unlimited
max user processes              (-u) 62797
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

为硬限制 只需通过添加行到文件来增加限制:/etc/security/limits.conf

root soft  nofile 40000

root hard  nofile 40000