Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: try fix script cleanup #43

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/amplitude/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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)