Skip to content

Commit 2917013

Browse files
author
Fathy Boundjadj
authored
Add sync_mode option (#147)
Adds a `sync_mode` option, this option prevents the Consumer thread from being created and directly makes a blocking HTTP request every call. Fixes #101. Closes #105, #113.
1 parent 301ca25 commit 2917013

3 files changed

Lines changed: 38 additions & 15 deletions

File tree

analytics/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
on_error = None
1111
debug = False
1212
send = True
13+
sync_mode = False
1314

1415
default_client = None
1516

@@ -58,7 +59,7 @@ def _proxy(method, *args, **kwargs):
5859
global default_client
5960
if not default_client:
6061
default_client = Client(write_key, host=host, debug=debug, on_error=on_error,
61-
send=send)
62+
send=send, sync_mode=sync_mode)
6263

6364
fn = getattr(default_client, method)
6465
fn(*args, **kwargs)

analytics/client.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from analytics.utils import guess_timezone, clean
1111
from analytics.consumer import Consumer
12+
from analytics.request import post
1213
from analytics.version import VERSION
1314

1415
try:
@@ -26,30 +27,37 @@ class Client(object):
2627

2728
def __init__(self, write_key=None, host=None, debug=False, max_queue_size=10000,
2829
send=True, on_error=None, upload_size=100, upload_interval=0.5,
29-
gzip=False, max_retries=10):
30+
gzip=False, max_retries=10, sync_mode=False):
3031
require('write_key', write_key, string_types)
3132

3233
self.queue = queue.Queue(max_queue_size)
33-
self.consumer = Consumer(self.queue, write_key, host=host, on_error=on_error,
34-
upload_size=upload_size, upload_interval=upload_interval,
35-
gzip=gzip, retries=max_retries)
3634
self.write_key = write_key
3735
self.on_error = on_error
3836
self.debug = debug
3937
self.send = send
38+
self.sync_mode = sync_mode
39+
self.host = host
40+
self.gzip = gzip
4041

4142
if debug:
4243
self.log.setLevel(logging.DEBUG)
4344

44-
# if we've disabled sending, just don't start the consumer
45-
if send:
46-
# On program exit, allow the consumer thread to exit cleanly.
47-
# This prevents exceptions and a messy shutdown when the interpreter is
48-
# destroyed before the daemon thread finishes execution. However, it
49-
# is *not* the same as flushing the queue! To guarantee all messages
50-
# have been delivered, you'll still need to call flush().
51-
atexit.register(self.join)
52-
self.consumer.start()
45+
if sync_mode:
46+
self.consumer = None
47+
else:
48+
self.consumer = Consumer(self.queue, write_key, host=host, on_error=on_error,
49+
upload_size=upload_size, upload_interval=upload_interval,
50+
gzip=gzip, retries=max_retries)
51+
52+
# if we've disabled sending, just don't start the consumer
53+
if send:
54+
# On program exit, allow the consumer thread to exit cleanly.
55+
# This prevents exceptions and a messy shutdown when the interpreter is
56+
# destroyed before the daemon thread finishes execution. However, it
57+
# is *not* the same as flushing the queue! To guarantee all messages
58+
# have been delivered, you'll still need to call flush().
59+
atexit.register(self.join)
60+
self.consumer.start()
5361

5462
def identify(self, user_id=None, traits=None, context=None, timestamp=None,
5563
anonymous_id=None, integrations=None, message_id=None):
@@ -228,12 +236,18 @@ def _enqueue(self, msg):
228236
if not self.send:
229237
return True, msg
230238

239+
if self.sync_mode:
240+
self.log.debug('enqueued with blocking %s.', msg['type'])
241+
post(self.write_key, self.host, gzip=self.gzip, batch=[msg])
242+
243+
return True, msg
244+
231245
try:
232246
self.queue.put(msg, block=False)
233247
self.log.debug('enqueued %s.', msg['type'])
234248
return True, msg
235249
except queue.Full:
236-
self.log.warn('analytics-python queue is full')
250+
self.log.warning('analytics-python queue is full')
237251
return False, msg
238252

239253
def flush(self):

analytics/test/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ def test_shutdown(self):
256256
self.assertTrue(client.queue.empty())
257257
self.assertFalse(client.consumer.is_alive())
258258

259+
def test_synchronous(self):
260+
client = Client('testsecret', sync_mode=True)
261+
262+
success, message = client.identify('userId')
263+
self.assertIsNone(client.consumer)
264+
self.assertTrue(client.queue.empty())
265+
self.assertTrue(success)
266+
259267
def test_overflow(self):
260268
client = Client('testsecret', max_queue_size=1)
261269
# Ensure consumer thread is no longer uploading

0 commit comments

Comments
 (0)