我正在運行多線程python腳本。它所做的是抓取網頁並將其插入/更新到MySQL中。這裏是我的代碼python:Mac OS X. malloc錯誤。被釋放的指針未被分配。中止陷阱6
mythread.py
import threading
import time
class MyThread (threading.Thread):
def __init__(self, threadID, threadname, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.threadname = threadname
self.queue = q
self.__exitFlag = False
self.__signal_lock = threading.Lock()
def run(self):
print "Starting " + self.threadname
self.process_data()
print "Exiting " + self.threadname
def stop(self):
with self.__signal_lock:
self.__exitFlag = True
def process_data(self):
while not self.__exitFlag:
if not self.queue.empty():
data = self.queue.get()
# crawl data from the web...
# update to mysql
# assuming we have already connected mysql:
# db = MySQLDb()
# db.connect
query = ""
db.query(query)
mysql_db.py
class MySQLDb:
conn = None
def connect(self):
self.conn = MySQLdb.connect(
host="127.0.0.1",
user = "root",
passwd = "password",
db = "moviestats")
self.cursor = self.conn.cursor(MySQLdb.cursors.DictCursor)
def query(self, sql):
try:
self.cursor.execute(sql)
self.conn.commit()
except (AttributeError, MySQLdb.OperationalError):
# solution to: MySQL server has gone away
self.cursor.close()
self.connect()
self.cursor = self.conn.cursor(MySQLdb.cursors.DictCursor)
self.cursor.execute(sql)
self.conn.commit()
以下是錯誤日誌:
Process: Python [905]
Path: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Identifier: Python
Version: 2.7.7 (2.7.7)
Code Type: X86-64 (Native)
Parent Process: bash [751]
Responsible: Terminal [410]
User ID: 501
Date/Time: 2014-07-09 22:31:43.221 +0800
OS Version: Mac OS X 10.9.3 (13D65)
Report Version: 11
....
....
Crashed Thread: 5
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Application Specific Information:
abort() called
*** error for object 0x100a4b600: pointer being freed was not allocated
......
Thread 5 Crashed:
0 libsystem_kernel.dylib 0x00007fff83153866 __pthread_kill + 10
1 libsystem_pthread.dylib 0x00007fff8de8735c pthread_kill + 92
2 libsystem_c.dylib 0x00007fff8ef88b1a abort + 125
3 libsystem_malloc.dylib 0x00007fff8220707f free + 411
4 libmysqlclient.18.dylib 0x0000000101027302 vio_delete + 44
5 libmysqlclient.18.dylib 0x000000010100709a end_server + 48
6 libmysqlclient.18.dylib 0x0000000101006f81 cli_safe_read + 49
7 libmysqlclient.18.dylib 0x000000010100b469 cli_read_query_result + 26
8 libmysqlclient.18.dylib 0x000000010100a648 mysql_real_query + 83
9 _mysql.so 0x0000000100533be8 _mysql_ConnectionObject_query + 85
10 org.python.python 0x00000001000c2fad PyEval_EvalFrameEx + 21405
11 org.python.python 0x00000001000c3bfa PyEval_EvalFrameEx + 24554
12 org.python.python 0x00000001000c3bfa PyEval_EvalFrameEx + 24554
13 org.python.python 0x00000001000c4fb3 PyEval_EvalCodeEx + 2115
14 org.python.python 0x00000001000c33f0 PyEval_EvalFrameEx + 22496
15 org.python.python 0x00000001000c3bfa PyEval_EvalFrameEx + 24554
16 org.python.python 0x00000001000c3bfa PyEval_EvalFrameEx + 24554
17 org.python.python 0x00000001000c4fb3 PyEval_EvalCodeEx + 2115
18 org.python.python 0x00000001000c33f0 PyEval_EvalFrameEx + 22496
19 org.python.python 0x00000001000c3bfa PyEval_EvalFrameEx + 24554
20 org.python.python 0x00000001000c3bfa PyEval_EvalFrameEx + 24554
21 org.python.python 0x00000001000c3bfa PyEval_EvalFrameEx + 24554
22 org.python.python 0x00000001000c4fb3 PyEval_EvalCodeEx + 2115
23 org.python.python 0x000000010003eac0 function_call + 176
24 org.python.python 0x000000010000ceb2 PyObject_Call + 98
25 org.python.python 0x000000010001f56d instancemethod_call + 365
26 org.python.python 0x000000010000ceb2 PyObject_Call + 98
27 org.python.python 0x00000001000bc957 PyEval_CallObjectWithKeywords + 87
28 org.python.python 0x0000000100102f27 t_bootstrap + 71
29 libsystem_pthread.dylib 0x00007fff8de86899 _pthread_body + 138
30 libsystem_pthread.dylib 0x00007fff8de8672a _pthread_start + 137
31 libsystem_pthread.dylib 0x00007fff8de8afc9 thread_start + 13
我跑了劇本與50個線程。發生的錯誤是間歇性的,但它是可重複的。我縮小了這個問題,這是由於插入/更新到MySQL。我讀到它可能是由於併發問題,但我如何解決它?
Python代碼不應該轉儲核心。如果你遇到這樣的崩潰,這是MySQL綁定中的一個錯誤。一旦找到原因,你應該向MySQL-python人員提交一個bug。 – nmichaels
tks快速回復。我將進一步調試在mysql – nuttynibbles