4
4
import threading
5
5
from datetime import datetime , timedelta
6
6
from mixpanel import BufferedConsumer as SynchronousBufferedConsumer
7
+ from mixpanel import MixpanelException
7
8
8
9
class FlushThread (threading .Thread ):
9
10
'''
@@ -44,18 +45,18 @@ class AsyncBufferedConsumer(SynchronousBufferedConsumer):
44
45
45
46
Because AsyncBufferedConsumer holds events until the `flush_after` timeout
46
47
or an endpoint queue hits the size of _max_queue_size, you should call
47
- flush(async=False) before you terminate any process where you have been
48
+ flush(async=False) before you terminate any process where you have been
48
49
using the AsyncBufferedConsumer.
49
50
'''
50
51
51
52
# constants used in the _should_flush method
52
53
ALL = "ALL"
53
54
ENDPOINT = "ENDPOINT"
54
55
55
- def __init__ (self , flush_after = timedelta (0 , 10 ), flush_first = True , max_size = 20 ,
56
+ def __init__ (self , flush_after = timedelta (0 , 10 ), flush_first = True , max_size = 20 ,
56
57
events_url = None , people_url = None , * args , ** kwargs ):
57
58
'''
58
- Create a new instance of a AsyncBufferedConsumer class.
59
+ Create a new instance of a AsyncBufferedConsumer class.
59
60
60
61
:param flush_after (datetime.timedelta): the time period after which
61
62
the AsyncBufferedConsumer will flush the events upon receiving a
@@ -68,12 +69,12 @@ def __init__(self, flush_after=timedelta(0, 10), flush_first=True, max_size=20,
68
69
:param people_url: the Mixpanel API URL that people events will be sent to
69
70
'''
70
71
super (AsyncBufferedConsumer , self ).__init__ (
71
- max_size = max_size ,
72
- events_url = events_url ,
72
+ max_size = max_size ,
73
+ events_url = events_url ,
73
74
people_url = people_url
74
75
)
75
76
76
- # remove the minimum max size that the SynchronousBufferedConsumer
77
+ # remove the minimum max size that the SynchronousBufferedConsumer
77
78
# class sets
78
79
self ._max_size = max_size
79
80
self .flush_after = flush_after
@@ -149,7 +150,7 @@ def send(self, endpoint, json_message):
149
150
:raises: MixpanelException
150
151
'''
151
152
if endpoint not in self ._async_buffers :
152
- raise MixpanelException ('No such endpoint "{0}". Valid endpoints are one of {1}' .format (self ._async_buffers .keys ()))
153
+ raise MixpanelException ('No such endpoint "{0}". Valid endpoints are one of {1}' .format (endpoint , self ._async_buffers .keys ()))
153
154
154
155
buf = self ._async_buffers [endpoint ]
155
156
buf .append (json_message )
@@ -175,8 +176,8 @@ def flush(self, endpoint=None, async=True):
175
176
thrown will have a message property, containing the text of the message,
176
177
and an endpoint property containing the endpoint that failed.
177
178
178
-
179
- :param endpoint (str): One of 'events' or 'people, the Mixpanel endpoint
179
+
180
+ :param endpoint (str): One of 'events' or 'people, the Mixpanel endpoint
180
181
for sending the data
181
182
:param async (bool): Whether to flush the data in a seperate thread or not
182
183
'''
@@ -209,7 +210,7 @@ def flush(self, endpoint=None, async=True):
209
210
# event is added this second flush will be retriggered and
210
211
# will complete.
211
212
flushing = False
212
-
213
+
213
214
else :
214
215
self .transfer_buffers (endpoint = endpoint )
215
216
self ._sync_flush (endpoint = endpoint )
@@ -226,17 +227,17 @@ def transfer_buffers(self, endpoint=None):
226
227
Transfer events from the `_async_buffers` where they are stored to the
227
228
`_buffers` where they will be flushed from by the flushing thread.
228
229
229
- :param endpoint (str): One of 'events' or 'people, the Mixpanel endpoint
230
+ :param endpoint (str): One of 'events' or 'people, the Mixpanel endpoint
230
231
that is about to be flushed
231
232
"""
232
- if endpoint :
233
+ if endpoint :
233
234
keys = [endpoint ]
234
235
else :
235
236
keys = self ._async_buffers .keys ()
236
237
237
238
for key in keys :
238
239
buf = self ._async_buffers [key ]
239
- while buf :
240
+ while buf :
240
241
self ._buffers [key ].append (buf .pop (0 ))
241
242
242
243
0 commit comments