"Sometimes" getting an InterfaceError on PostgreSQL with Peewee ORM
Asked Answered
B

0

8

I'm building a website using the Python Flask framework and the Peewee ORM with PostgreSQL 9.3. So far things are going pretty well, but I now run into some trouble. I sometimes get an InterfaceError: connection already closed. The code on which I get it is as follows:

pendingOrders_q = CustomerOrder\
    .select()\
    .where(CustomerOrder.status == CustomerOrder.STATUS_PENDING)\
    .where(CustomerOrder.expiration < datetime.utcnow())

if len(list(pendingOrders_q)) > 0:
    # do some stuff here..

I know that my way of counting the number of results is pretty nasty, but I don't know any other ways of doing it. Also, this normally works, but sometimes it gives the error below. I don't understand why it normally works fine, but sometimes gives this error. Does anybody know whats going on here?

  File "/home/kramer65/xs/app/order_management.py", line 210, in checkExpiration
    if len(list(pendingOrders_q)) > 0:
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1988, in __iter__
    return iter(self.execute())
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1981, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1698, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/usr/local/lib/python2.7/dist-packages/playhouse/postgres_ext.py", line 251, in execute_sql
    self.commit()
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 2104, in __exit__
    reraise(new_type, new_type(*exc_value.args), traceback)
  File "/usr/local/lib/python2.7/dist-packages/playhouse/postgres_ext.py", line 242, in execute_sql
    cursor = self.get_cursor()
  File "/usr/local/lib/python2.7/dist-packages/playhouse/postgres_ext.py", line 229, in get_cursor
    return self.get_conn().cursor(name=name)
InterfaceError: connection already closed

[EDIT] I now suddenly also get the following error for that line:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/apscheduler/scheduler.py", line 512, in _run_job
    retval = job.func(*job.args, **job.kwargs)
  File "/home/kramer65/xs/app/order_management.py", line 207, in checkForDetails
    for order in paidInOrders_q:
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1411, in next
    obj = self.iterate()
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1392, in iterate
    row = self.cursor.fetchone()
InterfaceError: cursor already closed
Brazen answered 6/4, 2014 at 12:26 Comment(2)
Also, it causes with pymysql engine.Subadar
Could you share your peewee setup code as well as your flask setup code and peewee model ?Ul

© 2022 - 2024 — McMap. All rights reserved.