diff options
| author | Devendra | 2014-04-30 17:14:09 +0530 |
|---|---|---|
| committer | Devendra | 2014-04-30 17:14:09 +0530 |
| commit | 10f345fcf4edb489cf9b86039c030baa62a6881c (patch) | |
| tree | 222417d67b02affa861351042e98075eaeeed2ea | |
| parent | e2ccbc05b6ed2696dd8d2d2134b2023c528e1b70 (diff) | |
| download | pubnub-python-10f345fcf4edb489cf9b86039c030baa62a6881c.tar.bz2 | |
adding persistent connectino support
| -rw-r--r-- | Pubnub.py | 23 | ||||
| -rwxr-xr-x | python/examples/publish-example.py | 40 |
2 files changed, 29 insertions, 34 deletions
@@ -46,6 +46,11 @@ try: except ImportError: import urllib2 +try: + import requests +except ImportError: + pass + import threading from threading import current_thread @@ -1092,6 +1097,18 @@ def _urllib_request_2(url, timeout=320): return (resp.read(), resp.code) +s = requests.Session() +def _requests_request(url, timeout=320): + try: + resp = s.get(url) + except requests.exceptions.HTTPError as http_error: + resp = http_error + except requests.exceptions.ConnectionError as error: + msg = {"message": str(error.reason)} + return (json.dumps(msg), 0) + + return (resp.text, resp.status_code) + def _urllib_request_3(url, timeout=320): try: @@ -1116,7 +1133,8 @@ class PubnubAsync(PubnubCoreAsync): auth_key=None, ssl_on=False, origin='pubsub.pubnub.com', - pres_uuid=None + pres_uuid=None, + pooling=True ): super(PubnubAsync, self).__init__( publish_key=publish_key, @@ -1136,6 +1154,9 @@ class PubnubAsync(PubnubCoreAsync): else: _urllib_request = _urllib_request_3 + if pooling is True: + _urllib_request = _requests_request + def timeout(self, interval, func): def cb(): time.sleep(interval) diff --git a/python/examples/publish-example.py b/python/examples/publish-example.py index 1c57e4b..78b7f36 100755 --- a/python/examples/publish-example.py +++ b/python/examples/publish-example.py @@ -21,7 +21,7 @@ ssl_on = len(sys.argv) > 6 and bool(sys.argv[5]) or False ## Initiate Pubnub State ## ----------------------------------------------------------------------- pubnub = Pubnub( - publish_key, subscribe_key, secret_key, cipher_key, auth_key, ssl_on) + publish_key, subscribe_key, secret_key, cipher_key, auth_key, ssl_on, pooling=True) crazy = 'hello_world' ## ----------------------------------------------------------------------- @@ -36,35 +36,9 @@ def publish_complete(info): def publish_error(info): print('ERROR : ' + str(info)) -## Publish string -pubnub.publish({ - 'channel': crazy, - 'message': 'Hello World!', - 'callback': publish_complete, - 'error': publish_error -}) - -## Publish list -li = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', - 'Saturday'] - -pubnub.publish({ - 'channel': crazy, - 'message': li, - 'callback': publish_complete, - 'error': publish_error -}) - - -def done_cb(info): - publish_complete(info) - -pubnub.publish({ - 'channel': crazy, - 'message': {'some_key': 'some_val'}, - 'callback': done_cb, - 'error': publish_error -}) - - -pubnub.start() +import time +start = time.time() +for i in range(1,100): + print pubnub.publish(crazy, 'hello world-' + str(i)) +end = time.time() +print(end - start); |
