Python Threading执行SQL语句时出错

时间:2012-05-30 19:42:24

标签: python sql-server multithreading

我有一个生产者,消费者应用程序,生产者读取数据库并将结果放入网站。成功时,消费者然后获取事务的id并更新db。

程序根据需要运行,直到它尝试执行更新。它有时会出现错误'HY000','驱动程序没有提供错误!'

代码可以轻松地写入文件而不会出现任何问题。

我该怎么做才能解决这个问题?我们需要更新数据库。

由于 注意......在mssql 2008上使用python 2.7和pyodbc。

下面的代码

    #!/usr/bin/env python

    from urlparse import urlparse
    from threading import Thread
    import httplib, sys
    import Queue
    import urllib2
    import urllib
    from time import localtime, strftime
    from ConfigParser import SafeConfigParser
    from functions import Functions
    from pyodbcclass import db_mssql

    now = strftime("%y-%m-%d-%H%M%S", localtime())
    k = db_mssql()


    thread_list = []
    thread_list2 = []
    getFromDBQueue = Queue.Queue()
    updateDBQueue = Queue.Queue()
    number_of_consumer_threads = 3

    def putURL():
        querySql = "select distinct top 3 id,url from tblURL where processed=0  order by id asc"

        re = k.query2(querySql)
        if re:

            for r in re:
                id = r.id
                params = urllib.urlencode({'user': user, 'password': password})
                ourl = urlini + "?%s" % params
                urlplusid = {'url':ourl.strip(),'id':id}
                getFromDBQueue.put(urlplusid)

    def getURL(thread_id):
        while 1:
            try:
                URL_toget = getFromDBQueue.get(block=False)
                url2 = URL_toget['url']
                msgid2 = URL_toget['id']
            except Queue.Empty:
                print "thread exiting, id: " + str(thread_id) + "++getFromDB++"
                sys.exit()
            status,url = getStatus(url2)
            if status == 200:
                updateDBQueue.put(msgid2)

            print(status)

    def updateDB(thread_id):
        while 1:
            try:

                id2 = updateDBQueue.get(block=False)
                if id2:
                    params = ['true',id2]
                    sqlupdate = "UPDATE tblURL SET processed=? WHERE id=?"
                    k.execute3(sqlupdate,params)

            except Queue.Empty:
                print "thread exiting, id: " + str(thread_id) + "**update**"
                sys.exit()


    # fill the queue with work and block until we are done filling the queue

    producer_thread = Thread(target=putURL)
    producer_thread.start()
    producer_thread.join()

    # we can now start consumers

    for i in range(number_of_consumer_threads):
        getfromDB = Thread(target=getURL, args=(i,))
        getfromDB.start()
        thread_list.append(getfromDB)


    for i in range(number_of_consumer_threads):
        update = Thread(target=updateDB, args=(i,))
        update.start()
        thread_list2.append(update)


    for thread in thread_list:
        thread.join()

    for thread2 in thread_list2:
        thread2.join()

0 个答案:

没有答案