MagicStack / MagicStack/asyncpg

[BUG] Bare connections cannot be used as context managers

Offen
#583 2 Kommentare 24 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
8.1k
Forks
468
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

  • asyncpg version: 0.20.1
  • PostgreSQL version: 10.3
  • Python version: 3.8.2
  • Platform: Windows
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes

While under most use cases, people will want to use connection pools to manage their connections, there are times where using bare connections turns out to be more practical. However, while connection pools can be opened and closed via context managers:

async with asyncpg.create_pool(user='postgres', command_timeout=60) as pool:
    async with pool.acquire() as con:
        await con.fetch('SELECT 1')

...it appears that bare connections created via asyncpg.connect do not support this:

async with asyncpg.connect(user='postgres') as con:
    await con.fetch('SELECT 1')
File "./test.py", line 120, in fetch_ip_list
    async with asyncpg.connect(user="postgres") as con:
AttributeError: __aexit__

I personally like using with blocks to handle the scope of resources like this, so this is a step down for me in terms of ergonomics, not to mention rather surprising given that connections coming from connection pools do work as context managers (even though those are merely returned to the pool instead of closed).

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne beim Einstiegspunkt asyncpg.connect und vergleiche die zurückgegebene Verbindung mit dem im Issue gezeigten Verhalten von pool und dem Context Manager pool.acquire. Überprüfe den erwarteten async with-Lebenszyklus für eine einzelne Verbindung und füge eine gezielte Abdeckung für das gemeldete Beispiel hinzu; fertig ist es, wenn das Beispiel ohne AttributeError ein- und austritt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
postgresql, python
Bereich
databases
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.