Unverified Commit a86e8071 authored by Hongbin Sun's avatar Hongbin Sun Committed by GitHub
Browse files

[Fix] LmdbBackend can't pickle env obj (#1764)

* fix bug of can't pickle env objs

* use utf-8 to support Chinese

* use __del__ instead close

* move try import to init

* remove getitem

* use client instead
parent d126f302
...@@ -474,17 +474,16 @@ class LmdbBackend(BaseStorageBackend): ...@@ -474,17 +474,16 @@ class LmdbBackend(BaseStorageBackend):
readahead=False, readahead=False,
**kwargs): **kwargs):
try: try:
import lmdb import lmdb # NOQA
except ImportError: except ImportError:
raise ImportError('Please install lmdb to enable LmdbBackend.') raise ImportError('Please install lmdb to enable LmdbBackend.')
self.db_path = str(db_path) self.db_path = str(db_path)
self._client = lmdb.open( self.readonly = readonly
self.db_path, self.lock = lock
readonly=readonly, self.readahead = readahead
lock=lock, self.kwargs = kwargs
readahead=readahead, self._client = None
**kwargs)
def get(self, filepath): def get(self, filepath):
"""Get values according to the filepath. """Get values according to the filepath.
...@@ -492,14 +491,29 @@ class LmdbBackend(BaseStorageBackend): ...@@ -492,14 +491,29 @@ class LmdbBackend(BaseStorageBackend):
Args: Args:
filepath (str | obj:`Path`): Here, filepath is the lmdb key. filepath (str | obj:`Path`): Here, filepath is the lmdb key.
""" """
filepath = str(filepath) if self._client is None:
self._client = self._get_client()
with self._client.begin(write=False) as txn: with self._client.begin(write=False) as txn:
value_buf = txn.get(filepath.encode('ascii')) value_buf = txn.get(str(filepath).encode('utf-8'))
return value_buf return value_buf
def get_text(self, filepath, encoding=None): def get_text(self, filepath, encoding=None):
raise NotImplementedError raise NotImplementedError
def _get_client(self):
import lmdb
return lmdb.open(
self.db_path,
readonly=self.readonly,
lock=self.lock,
readahead=self.readahead,
**self.kwargs)
def __del__(self):
self._client.close()
class HardDiskBackend(BaseStorageBackend): class HardDiskBackend(BaseStorageBackend):
"""Raw hard disks storage backend.""" """Raw hard disks storage backend."""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment