Skip to content

Commit 8c2f0b7

Browse files
authored
fix: try fix script cleanup (#43)
1 parent df1d184 commit 8c2f0b7

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/amplitude/client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from amplitude.event import Revenue, BaseEvent, Identify, IdentifyEvent, GroupIdentifyEvent, EventOptions
1111
from amplitude.plugin import AmplitudeDestinationPlugin, ContextPlugin, Plugin
1212
from amplitude.timeline import Timeline
13+
import atexit
14+
import threading
1315

1416

1517
class Amplitude:
@@ -47,6 +49,7 @@ def __init__(self, api_key: str, configuration: Config = Config()):
4749
self.configuration.api_key = api_key
4850
self.__timeline = Timeline()
4951
self.__timeline.setup(self)
52+
self._register_on_exit()
5053
self.add(AmplitudeDestinationPlugin())
5154
self.add(ContextPlugin())
5255

@@ -167,3 +170,13 @@ def shutdown(self):
167170
"""Shutdown the client instance, not accepting new events, flush all events in buffer"""
168171
self.configuration.opt_out = True
169172
self.__timeline.shutdown()
173+
174+
def _register_on_exit(self):
175+
"""Internal method to clean up the client instance on main thread exit"""
176+
if hasattr(threading, "_register_atexit"):
177+
try:
178+
threading._register_atexit(self.shutdown)
179+
except Exception as e:
180+
self.configuration.logger.warning("register for exit fail")
181+
else:
182+
atexit.register(self.shutdown)

0 commit comments

Comments
 (0)