Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit 3c01a12

Browse files
authored
Merge pull request #2 from appsembler/shadinaif/use-multiprocessing
Use JoinableQueue for compatibility with Celery
2 parents 5b9d878 + 827868c commit 3c01a12

5 files changed

Lines changed: 54 additions & 17 deletions

File tree

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
name: ${{ matrix.tox-env }}
12+
strategy:
13+
matrix:
14+
include:
15+
- python-version: 3.8
16+
tox-env: py38
17+
- python-version: 3.5
18+
tox-env: py35
19+
steps:
20+
- uses: actions/checkout@v3
21+
- name: Set up Python
22+
uses: actions/setup-python@v2
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Install dependencies
26+
run: pip install tox
27+
- name: Test with tox
28+
run: tox -e ${{ matrix.tox-env }}

analytics/client.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
from analytics.consumer import Consumer
1212
from analytics.version import VERSION
1313

14-
try:
15-
import queue
16-
except:
17-
import Queue as queue
14+
from queue import Full
15+
from multiprocessing import JoinableQueue
1816

1917

2018
ID_TYPES = (numbers.Number, string_types)
@@ -28,7 +26,7 @@ def __init__(self, write_key=None, host=None, debug=False, max_queue_size=10000,
2826
send=True, on_error=None):
2927
require('write_key', write_key, string_types)
3028

31-
self.queue = queue.Queue(max_queue_size)
29+
self.queue = JoinableQueue(max_queue_size)
3230
self.consumer = Consumer(self.queue, write_key, host=host, on_error=on_error)
3331
self.write_key = write_key
3432
self.on_error = on_error
@@ -220,7 +218,7 @@ def _enqueue(self, msg):
220218
self.queue.put(msg, block=False)
221219
self.log.debug('enqueued %s.', msg['type'])
222220
return True, msg
223-
except queue.Full:
221+
except Full:
224222
self.log.warn('analytics-python queue is full')
225223
return False, msg
226224

analytics/consumer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
from analytics.version import VERSION
55
from analytics.request import post
66

7-
try:
8-
from queue import Empty
9-
except:
10-
from Queue import Empty
7+
from queue import Empty
8+
119

1210
class Consumer(Thread):
1311
"""Consumes the messages from the client's queue."""

analytics/test/consumer.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import unittest
22

3-
try:
4-
from queue import Queue
5-
except:
6-
from Queue import Queue
3+
from multiprocessing import JoinableQueue
74

85
from analytics.consumer import Consumer
96

107

118
class TestConsumer(unittest.TestCase):
129

1310
def test_next(self):
14-
q = Queue()
11+
q = JoinableQueue()
1512
consumer = Consumer(q, '')
1613
q.put(1)
1714
next = consumer.next()
1815
self.assertEqual(next, [1])
1916

2017
def test_next_limit(self):
21-
q = Queue()
18+
q = JoinableQueue()
2219
upload_size = 50
2320
consumer = Consumer(q, '', upload_size)
2421
for i in range(10000):
@@ -27,7 +24,7 @@ def test_next_limit(self):
2724
self.assertEqual(next, list(range(upload_size)))
2825

2926
def test_upload(self):
30-
q = Queue()
27+
q = JoinableQueue()
3128
consumer = Consumer(q, 'testsecret')
3229
track = {
3330
'type': 'track',

tox.ini

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[tox]
2+
envlist = py35,py38
3+
4+
[testenv]
5+
download = True
6+
7+
deps =
8+
python-dateutil==2.4.0
9+
pytest
10+
11+
commands =
12+
pytest ./analytics/test/client.py \
13+
./analytics/test/consumer.py \
14+
./analytics/test/module.py \
15+
./analytics/test/request.py \
16+
./analytics/test/utils.py

0 commit comments

Comments
 (0)