Skip to content

Commit

Permalink
enhance(client): use destructor instead of atexit
Browse files Browse the repository at this point in the history
  • Loading branch information
xuchuan committed Aug 29, 2022
1 parent cc8f14f commit c7fd316
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions client/starwhale/api/_impl/data_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
import sys
import json
import atexit
import base64
import struct
import urllib
Expand Down Expand Up @@ -711,14 +710,16 @@ def get_instance() -> "LocalDataStore":
ensure_dir(ds_path)

LocalDataStore._instance = LocalDataStore(str(ds_path))
atexit.register(LocalDataStore._instance.dump)
return LocalDataStore._instance

def __init__(self, root_path: str) -> None:
self.root_path = root_path
self.name_pattern = re.compile(r"^[A-Za-z0-9-_/ ]+$")
self.tables: Dict[str, MemoryTable] = {}

def __del__(self) -> None:
self.dump()

def update_table(
self,
table_name: str,
Expand Down Expand Up @@ -1018,7 +1019,6 @@ def __init__(self, table_name: str, key_column: str = "id") -> None:
self.data_store = get_data_store()
self.cond = threading.Condition()
self.setDaemon(True)
atexit.register(self.close)
self.start()

def __enter__(self) -> Any:
Expand All @@ -1027,10 +1027,12 @@ def __enter__(self) -> Any:
def __exit__(self, type: Any, value: Any, tb: Any) -> None:
self.close()

def __del__(self) -> None:
self.close()

def close(self) -> None:
with self.cond:
if not self.stopped:
atexit.unregister(self.close)
self.stopped = True
self.cond.notify()
self.join()
Expand Down

0 comments on commit c7fd316

Please sign in to comment.