This project is archived and is in readonly mode.
psycopg exceptions cannot be pickled
-
Daniele Varrazzo
- State changed from new to invalid
It's not possible to pickle a connection nor a cursor because the connection wraps an open socket, which has a state that can't be pickled. It's not just a problem of constructor signature as in the links provided.
Furthermore, don't try to share a connection across processes: it won't work (see the docs for relevant info). If you want to use psycopg with processing, make sure to create a connection in every process and return only the results from the cursor (well, you should do this with threads too, as cursors are not thread-safe whereas connections are).
-
Daniele Varrazzo
- State changed from invalid to open
Uhm... reading back, I see what you mean. Sorry, I see now you just want to pickle the exception.
I think it can be done if the cursor is dropped from it. Will think about that.
I didn't even know cursor were attached to exceptions...
-
Federico Di Gregorio
They are because when the exception propagates up the call stack you may want to issue a ROLLBACK from a piece of code that doesn't have an easy way to obtain the current connection/cursor.
-
Daniele Varrazzo
Yeah, makes entirely sense.
I think it would be fine if we set cursor to None on pickle: because of the exception, we can say nobody has ever used pickle + exception.cursor together.
-
Federico Di Gregorio
Agreed.