Skip to content

Commit

Permalink
Merge pull request #10 from sensorsdata/yjz_fix_batch_consumer
Browse files Browse the repository at this point in the history
[feature] 优化 batch_consumer 多线程问题
  • Loading branch information
YueJZSensorsData authored Nov 4, 2020
2 parents 3152deb + 488be6c commit 7f09507
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

**禁止一切基于神策数据 Python 开源 SDK 的所有商业活动!**
**禁止一切基于神策数据 Python 开源 SDK 的所有商业活动!**
25 changes: 14 additions & 11 deletions sensorsanalytics/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import urllib2
import urllib

SDK_VERSION = '1.10.1'
SDK_VERSION = '1.10.2'
batch_consumer_lock = threading.RLock()

try:
isinstance("", basestring)
Expand Down Expand Up @@ -633,21 +634,23 @@ def __init__(self, url_prefix, max_size=50, request_timeout=None):
self._max_size = min(50, max_size)

def send(self, json_message):
self._buffers.append(json_message)
if len(self._buffers) >= self._max_size:
self.flush()
with batch_consumer_lock:
self._buffers.append(json_message)
if len(self._buffers) >= self._max_size:
self.flush()

def flush(self):
"""
用户可以主动调用 flush 接口,以便在需要的时候立即进行数据发送。
"""
while self._buffers:
msg_list = self._buffers[:self._max_size]
self._do_request({
'data_list': self._encode_msg_list(msg_list),
'gzip': 1
})
self._buffers = self._buffers[self._max_size:]
with batch_consumer_lock:
while self._buffers:
msg_list = self._buffers[:self._max_size]
self._do_request({
'data_list': self._encode_msg_list(msg_list),
'gzip': 1
})
self._buffers = self._buffers[self._max_size:]

def close(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
long_description = fh.read()
setuptools.setup(
name="SensorsAnalyticsSDK",
version="1.10.1",
version="1.10.2",
author="Jianzhong YUE", # 项目作者
author_email="[email protected]",
description="This is the official Python SDK for Sensors Analytics.",
Expand Down

0 comments on commit 7f09507

Please sign in to comment.