fastasyncpg 0.0.6__tar.gz → 0.0.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastasyncpg
3
- Version: 0.0.6
3
+ Version: 0.0.7
4
4
  Summary: A simple wrapper for asyncpg
5
5
  Author-email: Jeremy Howard <github@jhoward.fastmail.fm>
6
6
  License: Apache-2.0
@@ -0,0 +1 @@
1
+ __version__ = "0.0.7"
@@ -41,6 +41,8 @@ d = { 'settings': { 'branch': 'main',
41
41
  'fastasyncpg.core.Table._rec': ('core.html#table._rec', 'fastasyncpg/core.py'),
42
42
  'fastasyncpg.core.Table._recs': ('core.html#table._recs', 'fastasyncpg/core.py'),
43
43
  'fastasyncpg.core.Table.c': ('core.html#table.c', 'fastasyncpg/core.py'),
44
+ 'fastasyncpg.core.Table.claim': ('core.html#table.claim', 'fastasyncpg/core.py'),
45
+ 'fastasyncpg.core.Table.claim_one': ('core.html#table.claim_one', 'fastasyncpg/core.py'),
44
46
  'fastasyncpg.core.Table.cols': ('core.html#table.cols', 'fastasyncpg/core.py'),
45
47
  'fastasyncpg.core.Table.count': ('core.html#table.count', 'fastasyncpg/core.py'),
46
48
  'fastasyncpg.core.Table.count_where': ('core.html#table.count_where', 'fastasyncpg/core.py'),
@@ -61,6 +63,8 @@ d = { 'settings': { 'branch': 'main',
61
63
  'fastasyncpg.core.Table.upsert': ('core.html#table.upsert', 'fastasyncpg/core.py'),
62
64
  'fastasyncpg.core.Table.upserts': ('core.html#table.upserts', 'fastasyncpg/core.py'),
63
65
  'fastasyncpg.core.Table.xtra': ('core.html#table.xtra', 'fastasyncpg/core.py'),
66
+ 'fastasyncpg.core._ClaimCtx': ('core.html#_claimctx', 'fastasyncpg/core.py'),
67
+ 'fastasyncpg.core._ClaimCtx.__init__': ('core.html#_claimctx.__init__', 'fastasyncpg/core.py'),
64
68
  'fastasyncpg.core._Col': ('core.html#_col', 'fastasyncpg/core.py'),
65
69
  'fastasyncpg.core._Col.__init__': ('core.html#_col.__init__', 'fastasyncpg/core.py'),
66
70
  'fastasyncpg.core._Col.__iter__': ('core.html#_col.__iter__', 'fastasyncpg/core.py'),
@@ -92,6 +96,7 @@ d = { 'settings': { 'branch': 'main',
92
96
  'fastasyncpg.core._pk_where': ('core.html#_pk_where', 'fastasyncpg/core.py'),
93
97
  'fastasyncpg.core._prep_row': ('core.html#_prep_row', 'fastasyncpg/core.py'),
94
98
  'fastasyncpg.core._process_row': ('core.html#_process_row', 'fastasyncpg/core.py'),
99
+ 'fastasyncpg.core._select_sql': ('core.html#_select_sql', 'fastasyncpg/core.py'),
95
100
  'fastasyncpg.core.all_dcs': ('core.html#all_dcs', 'fastasyncpg/core.py'),
96
101
  'fastasyncpg.core.col_def': ('core.html#col_def', 'fastasyncpg/core.py'),
97
102
  'fastasyncpg.core.columns_info': ('core.html#columns_info', 'fastasyncpg/core.py'),
@@ -8,7 +8,7 @@ __all__ = ['pg_to_py', 'py_to_pg', 'Results', 'FRecord', 'table_names', 'view_na
8
8
  # %% ../nbs/00_core.ipynb #222d751e
9
9
  from fastcore.utils import *
10
10
  from contextlib import asynccontextmanager
11
- import asyncpg
11
+ import asyncpg,traceback
12
12
  from asyncpg import connection,protocol
13
13
 
14
14
  # %% ../nbs/00_core.ipynb #cdb9cd9e
@@ -325,17 +325,22 @@ async def get(self:Table, pk):
325
325
  try: return await self[pk]
326
326
  except NotFoundError: return None
327
327
 
328
- # %% ../nbs/00_core.ipynb #fd2d38a2
329
- @patch
330
- async def rows_where(self:Table, where=None, where_args=None, order_by=None, select='*', limit=None, offset=None,
331
- as_cls=True, debug=False):
332
- "Iterate over rows matching where clause"
333
- where, args = _add_xtra(self, where, where_args or [])
334
- sql = f'SELECT {select} FROM {self}'
328
+ # %% ../nbs/00_core.ipynb #19069c39
329
+ def _select_sql(tbl, where, where_args, order_by=None, select='*', limit=None, offset=None, suffix=''):
330
+ where, args = _add_xtra(tbl, where, where_args or [])
331
+ sql = f'SELECT {select} FROM {tbl}'
335
332
  if where: sql += f' WHERE {where}'
336
333
  if order_by: sql += f' ORDER BY {order_by}'
337
334
  if limit: sql += f' LIMIT {limit}'
338
335
  if offset: sql += f' OFFSET {offset}'
336
+ if suffix: sql += f' {suffix}'
337
+ return sql, args
338
+
339
+ # %% ../nbs/00_core.ipynb #fd2d38a2
340
+ @patch
341
+ async def rows_where(self:Table, where=None, where_args=None, order_by=None, select='*', limit=None, offset=None, as_cls=True, debug=False):
342
+ "Iterate over rows matching where clause"
343
+ sql, args = _select_sql(self, where, where_args, order_by, select, limit, offset)
339
344
  if debug: print(sql)
340
345
  if as_cls: return await self._recs(sql, *args)
341
346
  return await self.db.q(sql, *args)
@@ -669,12 +674,14 @@ def from_meta(cls:Database, conn, db):
669
674
  if hasattr(tbl, 'cls'): res.table(name).cls = tbl.cls
670
675
  return res
671
676
 
672
- # %% ../nbs/00_core.ipynb #3e4e3b18
677
+ # %% ../nbs/00_core.ipynb #c6be6cb0
673
678
  @patch
674
679
  @asynccontextmanager
675
680
  async def acquire(self:Database):
676
- "Context manager yielding a Database on a single pool connection"
677
- async with self.conn.acquire() as conn: yield Database.from_meta(conn, self)
681
+ "Context manager yielding a Database on a single connection"
682
+ if isinstance(self.conn, asyncpg.pool.Pool):
683
+ async with self.conn.acquire() as conn: yield Database.from_meta(conn, self)
684
+ else: yield self
678
685
 
679
686
  # %% ../nbs/00_core.ipynb #f0b7826a
680
687
  @patch
@@ -683,3 +690,33 @@ async def transaction(self:Database):
683
690
  "Context manager yielding a transactional Database on a single connection"
684
691
  async with self.acquire() as db:
685
692
  async with db.conn.transaction(): yield db
693
+
694
+ # %% ../nbs/00_core.ipynb #de4c0d24
695
+ @patch
696
+ async def claim(self:Table, where=None, where_args=None, order_by=None, limit=1):
697
+ "Claim a row with SELECT ... FOR UPDATE SKIP LOCKED (use inside a transaction)"
698
+ sql, args = _select_sql(self, where, where_args, order_by, limit=limit, suffix='FOR UPDATE SKIP LOCKED')
699
+ return await self._rec(sql, *args)
700
+
701
+ # %% ../nbs/00_core.ipynb #b9695685
702
+ class _ClaimCtx:
703
+ def __init__(self):
704
+ self.evt = self.db = self.tb = self.exc = None
705
+ self.retry = self.failed = False
706
+
707
+ @patch
708
+ @asynccontextmanager
709
+ async def claim_one(self:Table, status_col='status', pending='pending', completed='completed', failed='failed', order_by=None):
710
+ "Claim a row and manage its status transitions"
711
+ p = _ClaimCtx()
712
+ async with self.db.transaction() as txn:
713
+ tbl = txn.t[self.name]
714
+ p.db = txn
715
+ p.evt = await tbl.claim(where=f'"{status_col}"=$1', where_args=[pending], order_by=order_by)
716
+ try: yield p
717
+ except Exception as e: p.failed, p.exc, p.tb = True, e, traceback.format_exc()
718
+ if p.evt is None: return
719
+ pk = self.pks[0]
720
+ new = failed if p.failed else (None if p.retry else completed)
721
+ stmt = f'UPDATE {self} SET "{status_col}"=$1 WHERE "{pk}"=$2'
722
+ if new: await txn.execute(stmt, new, get_field(p.evt, pk))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fastasyncpg
3
- Version: 0.0.6
3
+ Version: 0.0.7
4
4
  Summary: A simple wrapper for asyncpg
5
5
  Author-email: Jeremy Howard <github@jhoward.fastmail.fm>
6
6
  License: Apache-2.0
@@ -28,4 +28,6 @@ version = {attr = "fastasyncpg.__version__"}
28
28
  include = ["fastasyncpg"]
29
29
 
30
30
  [tool.nbdev]
31
+ allowed_metadata_keys = ['solveit']
32
+ allowed_cell_metadata_keys = ['solveit_ai']
31
33
  custom_sidebar = false
@@ -1 +0,0 @@
1
- __version__ = "0.0.6"
File without changes
File without changes
File without changes
File without changes