Permalink
Browse files

Adding a worker thread to prevent blocking

  • Loading branch information...
1 parent 76d1957 commit 7a7b3260a495d4e4e1141320a75e0ee5bf4dbb42 @abh1nav committed Dec 6, 2012
Showing with 44 additions and 11 deletions.
  1. +2 −1 CHANGES.txt
  2. +41 −9 gnippy/__init__.py
  3. +1 −1 setup.py
View
@@ -1,2 +1,3 @@
v0.1.0, 2012-12-06 -- Initial release.
-v0.1.1, 2012-12-06 -- Bugfix in constructor.
+v0.1.1, 2012-12-06 -- Bugfix in constructor.
+v0.1.2, 2012-12-06 -- Adding background worker to prevent blocking while data is being fetched.
View
@@ -1,22 +1,54 @@
+# -*- coding: utf-8 -*-
+# gnippy - GNIP for Python
+
+__title__ = 'gnippy'
+__version__ = '0.1.2'
+__author__ = 'Abhinav Ajgaonkar'
+__license__ = 'Apache 2.0'
+__copyright__ = 'Copyright 2012 Abhinav Ajgaonkar'
+
+import threading
import requests
class PowerTrackClient():
+ """
+ PowerTrackClient allows you to connect to the GNIP
+ power track stream and fetch data
+ """
+ def __init__(self, url, auth, callback):
+ self.url = url
+ self.auth = auth
+ self.callback = callback
- def __init__(self, url, auth, on_data_function):
+ def connect(self):
+ self.worker = Worker(self.url, self.auth, self.callback)
+ self.worker.setDaemon(True)
+ self.worker.start()
+
+ def disconnect(self):
+ self.worker.stop()
+ self.worker.join()
+
+
+class Worker(threading.Thread):
+ """ Background worker to fetch data without blocking """
+ def __init__(self, url, auth, callback):
+ super(Worker, self).__init__()
self.url = url
self.auth = auth
- self.on_data = on_data_function
- self.enabled = True
+ self.on_data = callback
+ self._stop = threading.Event()
+ def stop(self):
+ self._stop.set()
+
+ def stopped(self):
+ return self._stop.isSet()
- def connect(self):
+ def run(self):
r = requests.get(self.url, auth=self.auth, prefetch=False)
for line in r.iter_lines():
- if not self.enabled:
+ if self.stopped():
break
elif line:
self.on_data(line)
-
-
- def disconnect(self):
- self.enabled = False
View
@@ -2,7 +2,7 @@
setup(
name='gnippy',
- version='0.1.1',
+ version='0.1.2',
description='GNIP for Python.',
author='Abhinav Ajgaonkar',
author_email='abhinav316@gmail.com',

0 comments on commit 7a7b326

Please sign in to comment.