Skip to content

Commit

Permalink
enhance(client): use destructor instead of atexit (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuchuan authored Aug 31, 2022
1 parent 1febccf commit 6a2bec0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 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 @@ -713,14 +712,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 @@ -1026,7 +1027,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 @@ -1035,10 +1035,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
6 changes: 0 additions & 6 deletions client/tests/sdk/test_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import tempfile
import unittest
from unittest.mock import patch, MagicMock

from starwhale.utils import config as sw_config
from starwhale.consts import ENV_SW_CLI_CONFIG, ENV_SW_LOCAL_STORAGE
Expand All @@ -18,12 +17,7 @@ def setUp(self) -> None:
self.datastore_root = str(sw_config.SWCliConfigMixed().datastore_dir)
ensure_dir(self.datastore_root)

self.mock_atexit = patch("starwhale.api._impl.data_store.atexit", MagicMock())
self.mock_atexit.start()

def tearDown(self) -> None:
empty_dir(self.local_storage)
os.environ.pop(ENV_SW_CLI_CONFIG, "")
os.environ.pop(ENV_SW_LOCAL_STORAGE, "")

self.mock_atexit.stop()

0 comments on commit 6a2bec0

Please sign in to comment.