peiman F. peiman F. - 3 months ago 20
MySQL Question

server crash after my python script start to working

i have a python script that listen to tcp port and after received data and a small string processing save data in mysql database:

script running and start working really good but after a while , at first i got mysql connection error and then server hanged and go down after crash!!!

my code is this:

#!/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()


the server is clean and dont have any other software!

what is wrong with my code here?

Answer

this kind of server crash happend for pass max open file limit

Os's have a soft and hard limit to manage memory regarding max available files open process

and ports are cloning as a virtual file as buffer

you can check the limit like :

[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

and also

[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

for hard limit just increase limit byadding lines to file: /etc/security/limits.conf

root soft  nofile 40000

root hard  nofile 40000
Comments