2016-03-21 7 views
0

私は、UDPツイストサーバーを書き、次のコマンドでそれを実行しました:ツイストサーバがクラッシュし

nohup python Udpserver2.py & 

これは、最初はうまくいったが、それは誤りを持ってきて、1日後に墜落しました。 nohup.outでのエラー情報は次のとおりです。

Unhandled Error 
Traceback (most recent call last): 
    File "/usr/lib64/python2.7/site-packages/Twisted-15.4.0-py2.7-linux-x86_64.egg/twisted/python/log.py", line 84, in callWithContext 
    return context.call({ILogContext: newCtx}, func, *args, **kw) 
    File "/usr/lib64/python2.7/site-packages/Twisted-15.4.0-py2.7-linux-x86_64.egg/twisted/python/context.py", line 118, in callWithContext 
    return self.currentContext().callWithContext(ctx, func, *args, **kw) 
    File "/usr/lib64/python2.7/site-packages/Twisted-15.4.0-py2.7-linux-x86_64.egg/twisted/python/context.py", line 81, in callWithContext 
    return func(*args,**kw) 
    File "/usr/lib64/python2.7/site-packages/Twisted-15.4.0-py2.7-linux-x86_64.egg/twisted/internet/posixbase.py", line 597, in _doReadOrWrite 
    why = selectable.doRead() 
--- <exception caught here> --- 
    File "/usr/lib64/python2.7/site-packages/Twisted-15.4.0-py2.7-linux-x86_64.egg/twisted/internet/udp.py", line 248, in doRead 
    self.protocol.datagramReceived(data, addr) 
    File "UdpServer2.py", line 91, in datagramReceived 
    self.device_echo(data, str(host), int(port)) 
    File "UdpServer2.py", line 19, in device_echo 
    cur.execute(sql) 
    File "/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py", line 174, in execute 
    self.errorhandler(self, exc, value) 
    File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler 
    raise errorclass, errorvalue 
_mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away') 

私のPythonコードUdpServer2.pyは次のように構成されています

from twisted.internet.protocol import DatagramProtocol 
from twisted.internet import reactor 
import MySQLdb as mdb 

conn = mdb.connect(host='localhost', user='root', passwd='123456', db='kj') 

class KjEcho(DatagramProtocol): 
    def device_echo(self, msg, host, port): 
     device_no = msg[1:7] 
     sql = "select did from device where device_no='%s'" % (device_no) 

     cur = conn.cursor(mdb.cursors.DictCursor) 
     cur.execute(sql)          #line 19 here 

     if 0 == cur.rowcount: 
      ... 
     else: 
      ... 
     cur.close() 

    def startProtocol(self): 
     print 'kj_udp_server starting...' 

    def datagramReceived(self, data, (host, port)): 
     print "receive msg" 
     if(18 != len(data)): 
      print 'len err' 
      return 
     if('0x86' != hex(ord(data[0]))): 
      print '0x86 err' 
      return 
     if(0 == ord(data[15])): 
      print 'from device' 
      self.device_echo(data, str(host), int(port))  #line 91 here 
     else: 
      print 'from mobile' 
      self.mobile_echo(data, str(host), int(port)) 

reactor.listenUDP(6000, KjEcho()) 
reactor.run() 

ライン91とライン19は、理由が、おかげでそれを修正する方法を、上記に示しています。

注:マシンがたくさんあり、それぞれのマシンは20秒ごとに

+0

データベースサーバーが利用できないように見える –

+0

エラーは、MySQLサーバーが離れている/ダウンしていると言います... – Mai

答えて

0
あなたはMySQLの接続で中継することができません

(またはそのことについては、ソケット)の時間の不特定の期間に生きている残りのサーバーにメッセージを送信し、特にデータが流れていない場合は特にそうです。そのため、カーソルをフェッチするために再利用する1つの接続ではなく、カーソルとの接続を閉じて、必要なときに再度開きます。

だから、本質的に、あなたはconnを設定1で、このライン

cur = conn.cursor(mdb.cursors.DictCursor) 

をプレフィックスと、その後connを閉じ1、最も可能性の高いconn.close()

cur.close() 

次追加します。

+0

エラーが「接続が閉じられている」とは言えません。私が正しくリコールすれば、MySQLdbは維持されません。 – Mai

+0

@MaiはPython AFAIKのmysqlライブラリでどのように報告されているのですか、私は同じエラーが投げられたもののいくつかをテストしました。 http://dev.mysql.com/doc/refman/5.7/en/gone-away.html –

+0

で詳しく読むことができます興味深い...これはやや誤解を招く...私はこのエラーを見たことがないと思います。共有ありがとう: – Mai

0

おかげで、私はそれは大丈夫だろうと思う多く、舞、およびTymoteuszポール

class KjEcho(DatagramProtocol): 
    def device_echo(self, msg, host, port): 
     device_no = msg[1:7] 
     sql = "select did from device where device_no='%s'" % (device_no) 

     conn = mdb.connect(host='localhost', user='root', passwd='123456', db='kj') 
     cur = conn.cursor(mdb.cursors.DictCursor) 
     cur.execute(sql) 

     if 0 == cur.rowcount: 
      ... 
     else: 
      ... 
     cur.close() 
     conn.close() 

が、しかし、接続のために、オープンし、それが頻繁に閉じられ、接続プールを使用する方が良いですか?

+0

あなたは私の答えを使ったので、なぜそれを受け入れなかったのですか? –

+0

私はあなたの答えを受け入れました、思い出させるためにありがとう、私は初心者であり、やり方を知らなかった。 – liuhui

関連する問題