1

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

该程序将根据需要运行,直到它尝试执行更新。它有时会失败并出现此错误“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()
4

0 回答 0