psycopg2 - Unkeyed Connection

谁说胖子不能爱 提交于 2020-06-17 03:08:09

问题


I'm trying to concurrently insert items into a postgres table via ThreadedConnectionPool, but I keep getting psycopg2.pool.PoolError: trying to put unkeyed connection - not sure why this is happening. I've tried running it sequentially also but still getting the same error.

Essentially the code scrapes a website's sitemap for products and inserts the scraped items into a database.

Code:

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()

Main:

s = items()
s.scrape(3)

回答1:


You're seeing this error since you're passing None to the putconn() function. Source can be seen at: https://github.com/psycopg/psycopg2/blob/master/lib/pool.py

You should adjust your finally block to be:

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

I'v encountered the error after forcing a Connection Pool refresh, and had a line that attempted to call putconn(conn) on a connection from the old pool.



来源:https://stackoverflow.com/questions/48877824/psycopg2-unkeyed-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!