2017-09-18 3 views
0

私はmacOS Sierraを使用していますが、私は自分のMySQLレコードをscrapyと一緒に更新しようとしていますが、最近は2006年のMySQLが廃止されました。 this guideに続いて、クロールの途中です。ターミナルは、以下でクラッシュします:再接続しようとするとMySQL/Python端末がクラッシュする

クラッシュレポート:

python(85034,0x70000b397000) malloc: *** error for object 0x7fa52317c400: pointer being freed was not allocated 
*** set a breakpoint in malloc_error_break to debug 
python(85034,0x70000a78e000) malloc: *** error for object 0x7fa52317c400: double free 
*** set a breakpoint in malloc_error_break to debug 
python(85034,0x70000ab91000) malloc: *** error for object 0x7fa52317c400: double free 
*** set a breakpoint in malloc_error_break to debug 
Abort trap: 6 

コード:

def connect(self): 
    self.conn = MySQLdb.connect(host='127.0.0.1', 
       user='root', 
       passwd='<my password>', 
       db='jobs', 
       charset='utf8' 
       ) 
def checkRecord(self, query, params, msg): 
    try: 
     cursor = self.conn.cursor() 
     cursor.execute(query, params) 
     fetch = cursor.fetchone() 
     return fetch 
    except (MySQLdb.OperationalError): 
     logging.info("Attempting to reconnect for select query") 
     self.connect() 
     self.checkRecord(query, params, msg) 
    finally: 
     logging.info(msg) 
     cursor.close() 

def checkCategory(self, query, params, msg): 
    try: 
     cursor = self.conn.cursor() 
     cursor.execute(query, params) 
     fetch = cursor.fetchone() 
     return fetch[0] 
    except (MySQLdb.OperationalError): 
     logging.info("Attempting to reconnect for select query") 
     self.connect() 
     self.checkRecord(query, params, msg) 
    finally: 
     logging.info(msg) 
     cursor.close() 

def insertRecord(self, query, params, msg): 
    try: 
     cursor = self.conn.cursor() 
     cursor.execute(query, params) 
    except (MySQLdb.OperationalError): 
     logging.info("Attempting to reconnect for inserting record") 
     self.connect() 
     self.insertRecord(query, params, msg) 
    finally: 
     logging.info(msg) 
     cursor.close() 

def updateCategory(self, query, params, msg): 
    try: 
     cursor = self.conn.cursor() 
     cursor.execute(query, params) 
    except (MySQLdb.OperationalError): 
     logging.info("Attempting to reconnect for updating record") 
     self.connect() 
     self.updateCategory(query, params, msg)    
    finally: 
     self.conn.commit() 
     logging.info(msg) 
     cursor.close() 

def _conditional_insert(self,tx,item): 

    selectquery = "SELECT count(*) FROM dbJobs WHERE jobdetailsurl LIKE %s AND company_name LIKE %s AND title LIKE %s" 
    selectparams = (item['jobdetailsurl'], item['company_name'], item['title']) 
    msg = "Checking for whether record exists in Database" 

    fetch = self.checkRecord(selectquery, selectparams, msg) 
    .... 
    .... 

私はまた、Windows 10マシン上でこのコードをテストし、コマンドプロンプトがクラッシュしたとしても。この問題の解決策はありますか?

EDITは:(?十分で新しい接続を開くと、すべてのカーソルの後に接続を閉じます)私は、この問題を解決するために管理している

def checkRecord(self, query, params, msg): 
    try: 
     self.connect() 
     cursor = self.conn.cursor() 
     cursor.execute(query, params) 
     fetch = cursor.fetchone() 
     return fetch 
    except (MySQLdb.OperationalError): 
     logging.info("Attempting to reconnect for select query") 
     self.connect() 
     self.checkRecord(query, params) 
    finally: 
     cursor.close() 
     self.conn.close() 

    logging.info(msg) 
+0

チェックこれは、それが役立つことhttps://stackoverflow.com/questions/22971247/how-to-debug-c-compiler-errors-in-python-malloc-error – haifzhan

+0

ありがとうございました! – dythe

+0

そのような方法で(メインポストで編集されたように)実装すれば、そのリンクで説明されているような状況を防ぐのに十分でしょうか? – dythe

答えて

0

各クエリ/更新のための新しい接続を作成することによって、/ [挿入その後、すべてのクエリに対して1つの接続を残す代わりに、その接続を終了した後にその接続を閉じます。一晩中2台のマシンで稼働させてテストし、クラッシュはもう発生しませんでした。

例:

def checkRecord(self, query, params, msg): 
    try: 
     self.connect() 
     cursor = self.conn.cursor() 
     cursor.execute(query, params) 
     fetch = cursor.fetchone() 
     return fetch 
    except (MySQLdb.OperationalError): 
     logging.info("Attempting to reconnect for select query") 
     self.connect() 
     self.checkRecord(query, params) 
    finally: 
     cursor.close() 
     self.conn.close() 

    logging.info(msg) 
関連する問題