如何在 Python 中调试 C 编译器错误?(malloc 错误) [英] How to debug C compiler errors in Python? (malloc error)

查看:36
本文介绍了如何在 Python 中调试 C 编译器错误?(malloc 错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 中的 Cherrypy 进行 Web 开发.

I am doing web development using Cherrypy in Python.

我有一个没有错误的工作网页,但在我开始使用 Mako 作为前端代码对其进行参数化后,弹出以下错误消息.

I had a working web page that did not have errors, but after I started using Mako for the front-end codes to parametrize it, the following error message pops up.

Python quit unexpectedly while using the libmysqlclient.18.dylib plug-in.

它也在控制台上抛出以下错误.

also it's throwing the following error at the console.

127.0.0.1 - - [09/Apr/2014:11:20:00] "GET /submit_data?idx=2 HTTP/1.1" 200 5990 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36"
python(375,0x103980000) malloc: *** error for object 0x7fb5a4061000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

这似乎是 C 编译器的错误,可能是由于 MySQL,但我不知道出了什么问题.

It seems like an error from C compiler, and maybe due to MySQL, but I cannot figure out what is wrong.

我能做什么?我猜我可能以错误的方式使用 MySQL,我附上了 Python 代码片段,我在其中连接到 MySQL 数据库并使用它.

What can I do? I am guessing maybe I am using MySQL in a wrong way, I am attaching the Python code snippet in which I make connections to MySQL databases and use it.

dbdict = {}
for name in params["dashboards"]:
    for chart in params["dashboards"][name]:
        dbdict[chart["dbname"]] = None

def connect(thread_index): 
    for name in dbdict:
        db = params["db"][name]
        dbdict[name] = MySQLdb.connect(db["host"], db["user"], db["password"], db["dbname"])

cherrypy.engine.subscribe('start_thread', connect)

class AjaxApp(object):
    @cherrypy.expose
    @cherrypy.tools.mako(filename="index.html", directories=MEDIA_DIR)
    def index(name=None):
        return {'size': len(params["dashboards"]["first"])}

    @cherrypy.expose 
    def submit_data(self, idx):
        idx = int(idx)
        chart = params["dashboards"]["first"][idx]
        # Sample page that displays the number of records in "table" 
        # Open a cursor, using the DB connection for the current thread 
        c = dbdict[chart["dbname"]].cursor() 
        print 'query being executed......'
        c.execute(chart["command"])
        print 'query being fetched......'
        res = c.fetchall()
        c.close()

        # construct a JSON object from query result
        jsres = []
        jsres.append(chart["selected"])
        q_result = []
        for x in res:
            tmp_arr = []
            for i in x:
                if type(i) is datetime.datetime:
                    tmp_arr.append(str(i))
                else:
                    tmp_arr.append(i)
            q_result.append(tmp_arr)
        jsres.append(q_result)

        return json.dumps(jsres)

这里我连接到所有使用的数据库,并将它们放入一个python字典中,每当我运行查询命令时,我都会查找相应的数据库对象,并使用它进行查询.

Here I am connecting to all dbs that are used, and putting them in a python dictionary, and whenever I am running a query command, I look up the corresponding db object, and make queries using it.

现在我的 connect 函数看起来像这样

Now my connect function looks like this

def connect(thread_index): 
    for name in dbdict:
        print name
        db = params["db"][name]
        cherrypy.thread_data.db = MySQLdb.connect(db["host"], db["user"], db["password"], db["dbname"])
        dbdict[name] = cherrypy.thread_data.db

我遇到了同样的错误.

推荐答案

看起来您在多个线程之间共享一个连接(您在 connect()函数).这可能会导致意外的同步问题,尤其是在没有编写 C 库来处理它的情况下.尝试将您的连接对象附加到 cherrypy.thread_data,如此处所示.

It looks like you're sharing a single connection between multiple threads (you're creating a single connection for each db in your connect() function). This may cause unexpected synchronization issues, especially if a C library is not written to handle it. Try attaching your connection objects to cherrypy.thread_data as shown here.

这是我将如何编写它(未经测试):

Here's how I would write it (untested):

def connect(thread_index):
    cherrypy.thread_data.dbdict = dbdict = {}
    for name in params["dashboards"]:
        for chart in params["dashboards"][name]:
            dbdict[chart["dbname"]] = None
    for name in dbdict
        db = params["db"][name]
        dbdict[name] = MySQLdb.connect(db["host"], db["user"], db["password"], db["dbname"])

然后在submit_data()中:

@cherrypy.expose
def submit_data(self, idx):
    ...
    c = cherrypy.thread_data.dbdict[chart["dbname"]].cursor() 

这篇关于如何在 Python 中调试 C 编译器错误?(malloc 错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆