psycopg2 - 无键连接

时间:2018-02-20 04:22:56

标签: python postgresql psycopg2

我试图通过ThreadedConnectionPool同时将项目插入到postgres表中,但我不断得到psycopg2.pool.PoolError: trying to put unkeyed connection - 不确定为什么会发生这种情况。我已尝试按顺序运行它,但仍然遇到同样的错误。

基本上,代码会抓取网站的产品站点地图,并将已删除的项目插入数据库。

代码:

class items:

def __init__(self):
    self.conn = ThreadedConnectionPool(10, 100, dbname='postgres', user='xxx', password='xxx', host='xxx')
    self.url = "some url"
    self.session = requests.Session()

def scrape(self, pageNo):
    //some logic
    self.page(pageNo)

// scrapes specified page from sitemap
def page(self, page):
    resp = self.session.get(self.mens+"?page="+str(page)).json()
    products = resp['products']
    ts = []
    for item in products:
        # self.indivProduct(self.url + pageNo)
        t = threading.Thread(target=self.indivProduct, args=self.url + pageNo,))
        ts.append(t)
        t.start()
    for item in ts:
        item.join()

def indivProduct(self, url):

    conn = self.conn.getconn()
    cursor = conn.cursor()

    // Some logic with requests

    try:
        sql = 'insert into "Output" ' \
              '("productID", "brand", "categoryID", "productName", "price", "sizeInfo", "SKU", "URL", "dateInserted", "dateUpdated")' \
              'values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'

        cursor.execute(sql,
                            (.., .., ..,))
        conn.commit()
    except IntegrityError:
        conn.rollback()
        sql = 'insert into "Output" ' \
              '("productID", "brand", "categoryID", "productName", "price", "sizeInfo", "SKU", "URL", "dateInserted", "dateUpdated")' \
              'values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) on conflict ("productID") do update set "dateUpdated" = EXCLUDED."dateUpdated"'
        cursor.execute(sql,
                            (.., .., ..,))
        conn.commit()
    except Exception as e:
        print(e)
        print()
    finally:
        self.conn.putconn()

主:

s = items()
s.scrape(3)

1 个答案:

答案 0 :(得分:0)

由于将None传递给putconn()函数,因此您会看到此错误。 来源可以在以下位置查看: https://github.com/psycopg/psycopg2/blob/master/lib/pool.py

您应该将finally块调整为:

    finally:
        cursor.close()
        self.conn.putconn(conn)

强制刷新连接池后,我遇到了错误,并且有一行试图从旧池的连接上调用putconn(conn)。