-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
173 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from pypusu.polling import PuSuClient | ||
from time import sleep, time | ||
|
||
if __name__ == "__main__": | ||
print("Connecting") | ||
c = PuSuClient("ws://127.0.0.1:55000") | ||
|
||
count = 0 | ||
|
||
def listener(msg): | ||
global count | ||
count += 1 | ||
|
||
print("Authorizing") | ||
c.authorize("foo") | ||
print("Subscribing") | ||
c.subscribe("channel.1", listener) | ||
|
||
print("Waiting") | ||
|
||
target = 500 | ||
start = time() | ||
for i in range(1, target + 1): | ||
c.publish("channel.1", {"foo": "bar"}) | ||
end = time() | ||
elapsed = end - start | ||
|
||
print("Sent {} messages in {:.3f}s, {:.2f}msg/s".format( | ||
target, | ||
elapsed, | ||
target / elapsed | ||
)) | ||
|
||
sleep(1) | ||
print("So far got {} messages, polling...".format(count)) | ||
c.poll() | ||
print("After poll got {} messages, waiting for more...".format(count)) | ||
|
||
for i in range(0, 60): | ||
sleep(1) | ||
c.poll() | ||
|
||
print("Got {} messages".format(count)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import json | ||
from .threaded import PuSuClient as ThreadedPuSuClient | ||
|
||
try: | ||
# Py2 | ||
from Queue import Queue, Empty | ||
except ImportError: | ||
# Py3 | ||
from queue import Queue, Empty | ||
|
||
DEBUG = False | ||
|
||
|
||
class PuSuClient(ThreadedPuSuClient): | ||
""" | ||
PuSu Engine client using the ws4py builtin client. Variant where reading | ||
messages takes polling, in case you want to avoid your code being called | ||
from another thread. Just make sure you poll often enough or the queue | ||
will use up your RAM. | ||
.. code-block:: python | ||
from pypusy.polling import PuSuClient | ||
client = PuSuClient("ws://127.0.0.1:55000") | ||
def listener(msg): | ||
print(msg) | ||
client.subscribe("my-channel", listener) | ||
client.publish("some-channel", "data") | ||
# Will call any subscribers if there were messages to deliver | ||
client.poll() | ||
""" | ||
|
||
def __init__(self, *args): | ||
super(PuSuClient, self).__init__(*args) | ||
self._incoming_messages = Queue() | ||
|
||
def poll(self): | ||
""" | ||
Check for any new messages for us | ||
""" | ||
count = 0 | ||
while True: | ||
try: | ||
item = self._incoming_messages.get_nowait() | ||
count += 1 | ||
self._on_receive(item) | ||
except Empty: | ||
break | ||
if DEBUG: | ||
print("<< {} messages delivered from queue".format(count)) | ||
|
||
def _received_message(self, data): | ||
if DEBUG: | ||
print("<- {}".format(data)) | ||
|
||
msg = json.loads(str(data)) | ||
# Only publish messages should be paused, the rest is required for | ||
# normal function | ||
if msg["type"] == PuSuClient.TYPE_PUBLISH: | ||
self._incoming_messages.put(msg) | ||
else: | ||
self._on_receive(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters