2016-04-09 4 views
2

iがポートをTCPと受信データとmysqlデータベース内のデータのセーブ小さな文字列処理の後に耳を傾けるPythonスクリプトを持って作業を開始しますしばらくして、最初に私は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

、OSのメモリに関する最大利用可能なファイルオープンなプロセスを管理するためのソフトとハードの限界を持っている

とポートはバッファとして仮想ファイルとしてクローニングされています

あなたは次のように制限を確認することができます。

[[email protected]~]# 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 

とも

[[email protected]~]# 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 

ハードリミット だけでファイルに行をbyadding制限を増やすために:/etc/security/limits.conf

root soft nofile 40000 

root hard nofile 40000 
関連する問題