Issue: Interface erors with PostgreSQL on Ubuntu
Author: kramer65
State: closed
Created: 2014-04-06 14:25:20

I'm using (and loving) Peewee in a project with PostgreSQL 9.3 and since I want to use the JSON field in Postgres I use the Peewee Playhouse package. I now "sometimes" hit upon Interface errors. Sometimes in this case means that I run the same function every second (using APScheduler), but about 5% of the time I get errors. I so far have gotten the following two errors, of which the first errors happens most:

```
  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
```

And the second error:

```
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
```

Does anybody know whats wrong here?
I know that using len(list(q)) to do a count on the results from a query isn't very nice, but it used to work well for me until I hit this error.  Since it succeeds most of the time, I don't really know where to look for the error. Is it in Postgres, in Peewee, in psycopg2, or in my own code?

All tips are welcome!


I have no idea... Is your application multi-threaded?


Well, I indeed run one function using APScheduler while also calling it manually sometimes. I guess this could cause the function to be called twice simultaneously. Since you asked specifically about it, I guess this is a problem?


Interesting -- I'm guessing this is the problem. How are you instantiating your database? You might try passing `threadlocals=True`.


Can you try instantiating your database with `threadlocals=True` and getting back to me with whether that works?


I'm going to close this for now, please leave a comment if you have any updates.