From a3716cf2a17a019e2f7d4d20ec11b2bf27d81fcd Mon Sep 17 00:00:00 2001 From: Qingzhuo Zhen Date: Tue, 10 Jan 2023 18:35:47 -0800 Subject: [PATCH 1/2] fix: try fix script cleanup --- src/amplitude/client.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/amplitude/client.py b/src/amplitude/client.py index 2e519f2..80653a8 100644 --- a/src/amplitude/client.py +++ b/src/amplitude/client.py @@ -10,6 +10,8 @@ from amplitude.event import Revenue, BaseEvent, Identify, IdentifyEvent, GroupIdentifyEvent, EventOptions from amplitude.plugin import AmplitudeDestinationPlugin, ContextPlugin, Plugin from amplitude.timeline import Timeline +import atexit +import threading class Amplitude: @@ -47,6 +49,7 @@ def __init__(self, api_key: str, configuration: Config = Config()): self.configuration.api_key = api_key self.__timeline = Timeline() self.__timeline.setup(self) + self._register_on_exit() self.add(AmplitudeDestinationPlugin()) self.add(ContextPlugin()) @@ -167,3 +170,10 @@ def shutdown(self): """Shutdown the client instance, not accepting new events, flush all events in buffer""" self.configuration.opt_out = True self.__timeline.shutdown() + + def _register_on_exit(self): + """Internal method to clean up the client instance on main thread exit""" + if hasattr(threading, "_register_atexit"): + threading._register_atexit(self.shutdown) + else: + atexit.register(self.shutdown) From 87cfffab7e92ac6e4e214e33cd7cc4c4f5b60568 Mon Sep 17 00:00:00 2001 From: Qingzhuo Zhen Date: Tue, 10 Jan 2023 21:11:31 -0800 Subject: [PATCH 2/2] add try-catch block for safety --- src/amplitude/client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/amplitude/client.py b/src/amplitude/client.py index 80653a8..dded59f 100644 --- a/src/amplitude/client.py +++ b/src/amplitude/client.py @@ -174,6 +174,9 @@ def shutdown(self): def _register_on_exit(self): """Internal method to clean up the client instance on main thread exit""" if hasattr(threading, "_register_atexit"): - threading._register_atexit(self.shutdown) + try: + threading._register_atexit(self.shutdown) + except Exception as e: + self.configuration.logger.warning("register for exit fail") else: atexit.register(self.shutdown)