Skip to content

Commit aade974

Browse files
committed
reserve atexit for python3 only
1 parent bd7ef23 commit aade974

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

analytics/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import sys
12
from datetime import datetime
23
from uuid import uuid4
34
import logging
45
import numbers
5-
import atexit
66

77
from dateutil.tz import tzutc
88
from six import string_types
99

10-
from analytics.utils import guess_timezone, clean
10+
from analytics.utils import guess_timezone, clean, clean_exit
1111
from analytics.consumer import Consumer
1212
from analytics.version import VERSION
1313

@@ -45,7 +45,7 @@ def __init__(self, write_key=None, debug=False, max_queue_size=10000,
4545
# destroyed before the daemon thread finishes execution. However, it
4646
# is *not* the same as flushing the queue! To guarantee all messages
4747
# have been delivered, you'll still need to call flush().
48-
atexit.register(self.join)
48+
clean_exit(self.join)
4949
self.consumer.start()
5050

5151
def identify(self, user_id=None, traits=None, context=None, timestamp=None,

analytics/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numbers
66

77
import six
8+
import sys
89

910
log = logging.getLogger('segment')
1011

@@ -74,3 +75,17 @@ def _coerce_unicode(cmplx):
7475
except:
7576
raise
7677
return item
78+
79+
80+
def clean_exit(func):
81+
# ugly hack to get around
82+
# atexit's excentricities
83+
if (sys.version_info > (3, 0)):
84+
import atexit
85+
# This prevents exceptions and a messy shutdown when the interpreter is
86+
# destroyed before the daemon thread finishes execution. However, it
87+
# is *not* the same as flushing the queue! To guarantee all messages
88+
# have been delivered, you'll still need to call flush().
89+
atexit.register(func)
90+
else:
91+
sys.exitfunc = func

0 commit comments

Comments
 (0)