psycopg / psycogreen

Integration of psycopg2 with coroutine libraries

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for psycopg 3

dvarrazzo opened this issue · comments

Psycopg 3 requires some "psycogreen" support starting from 3.1.5 when the c or binary extensions are used.

Note: we were given useful suggestions in gevent/gevent#1942 and we should work on them.

@dvarrazzo do you plan on working on this issue in the near future?
I'm totally green on this subject so I cannot estimate the effort involved, is it a hard issue? is it a complicated issue for a beginner?

Hello @acangiani

The issue arose in Psycopg 3.1.5 because we replaced the psycopg.waiting.wait() function with a C version for performance reason. This function, however, cannot be monkey-patched.

If you look at the definition in the module, actually, the wait function can be selected according to different policies.

There are two ways to fix the issue: one is very simple, the other might be more complex.

The simple way is to change psycopg.waiting.wait to be a monkey-patchable function. The best choice I think is wait_selector, because it should work with all the platforms and, if gevent/eventlet monkey-patch selectors.DefaultSelector, the job is done.

The complex way is to write a wait function implementation specific for gevent/eventlet. This might be tricky, because the function needs to wait on both read and write (see my question above). There is also the risk that soon the interface for the wait function might have to change (because of the problem found in psycopg/psycopg#673).

So maybe the monkey patch function could be as simple as:

from psycopg import waiting
waiting.wait = waiting.wait_selector

Do you want to try something along this line?

Hi @dvarrazzo

Is that snippet above with swapping selectors working as intended and considered as the official solution moving forward? I'm not doing Python that often to track all the changes in ecosystems nowadays, but when I do, I do it with gevent, and I'd like to understand if psycogreen is still required as it used to be back in psycopg2 days?

@avanov

Is that snippet above with swapping selectors working as intended and considered as the official solution moving forward

The snipped above is not public api and may change in minor version without warning.

Currently it works, and adding psycopg 3 support to psycogreen may use that way. However we might change that implementation: should we do it we would change psycogreen accordingly. If someone use it, they are on their own should we break that interface.

@dvarrazzo that's good enough for me, thanks for clarifying it!

Psycopg 3.1.14 now supports gevent out of the box.

Support for Eventlet doesn't seem required (but can be easily added if requested)

@dvarrazzo thanks for updating this ticket and supporting Gevent out of the box in Psycopg 3.1.14.