python-daemonを使用してデーモンとして実行したい、arduino からのデータを postgresql データベースに保存するように python スクリプトを書き直しています。元のスクリプトは正常に動作しますが、デーモンではデータベースに書き込むことができません。最初の試行は次のようになります。
<class 'psycopg2.DatabaseError'>, DatabaseError('SSL SYSCALL error: EOF detected\n'
その後:
<class 'psycopg2.InterfaceError'>, InterfaceError('cursor already closed',)
作業スクリプトでは、次のことを行います。
connstring="dbname='"+dbdatabase+"' user='"+dbusername+"' host='"+dbhost+"'password='"+dbpassword+"'"
try:
conn = psycopg2.connect(connstring)
cur=conn.cursor()
except:
my_logger.critical(appname+": Unable to connect to the database")
sys.exit(2)
sql="insert into measure (sensorid,typeid,value) VALUES(%s,%s,%s)"
< more to set up serialport, logging and so on>
while 1:
< fetch a data set and split it to a list >
for (i,val) in enumerate measures:
try:
cur.execute(sql,(sensors[i],typeid[i],val))
conn.commit()
except:
self.logger.error(appname+": error 106 :"+str(sys.exc_info()))
これは、シリアル接続で最初に発生したのと同じ問題の一部である可能性があると感じています。シリアルポートは、書き換えられた Python コードでは機能しないため、いじってみましたfiles_preserve
。
self.files_preserve=range(daemon.daemon.get_maximum_file_descriptors()+1)
私が理解できる限り、すべてのファイルハンドルを開いたままにしておく必要がありますが、役に立ちません。
デーモンでは、最初にデータベース接続を の属性として設定しようとしました__init__
。
self.conn = psycopg2.connect(connstring)
self.cur=conn.cursor()
run
次に、メソッドで挿入を行います。run
メソッドの先頭に接続を作成し、それをグローバル オブジェクトとして設定することも試みましたが、いずれの場合も、何かがデータベース接続を強制終了しているようです。手がかりはありますか?(または、デーモン モジュールのドキュメント (ソース以外) を見つける手がかりはありますか?)
デーモンとデータベースの両方が、Python 2.7 and postgresql
8.4` を搭載した debian Linux システムで実行されています。