Skip to content

Commit

Permalink
chore: fix closing of LMDB cursor in LmdbStore
Browse files Browse the repository at this point in the history
  • Loading branch information
david20571015 committed Jun 6, 2024
1 parent e4df839 commit 23f23f4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sync_crawler/store/lmdb_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pickle
import uuid
from collections.abc import Callable, Iterable
from contextlib import closing
from typing import override

import lmdb
Expand Down Expand Up @@ -40,16 +41,19 @@ def put(self, news: Iterable[News]):

with (
self._env.begin(write=True) as txn,
txn.cursor() as cur,
closing(txn.cursor()) as cur,
):
cur.putmulti(key_value_pairs)

@override
def pop(self, nums=1) -> Iterable[News]:
values: Iterable[News] = []

with self._env.begin(write=True) as txn:
for key, value in itertools.islice(txn.cursor(), nums):
with (
self._env.begin(write=True) as txn,
closing(txn.cursor()) as cur,
):
for key, value in itertools.islice(cur, nums):
values.append(pickle.loads(value))
txn.delete(key)

Expand Down

0 comments on commit 23f23f4

Please sign in to comment.