Last active
May 23, 2016 12:08
-
-
Save mattbillenstein/7baa1d0d006cf6981942 to your computer and use it in GitHub Desktop.
gevent streamer thingy
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
#!/usr/bin/env python | |
import gevent.monkey | |
gevent.monkey.patch_all() | |
import gevent | |
import random | |
import time | |
from gevent.event import Event | |
from collections import deque | |
_evt_set = Event() | |
_evt_clr = Event() | |
_dq = deque() | |
WINDOW = 300 # seconds to keep old data, could use number of samples too | |
def getMeasurement(): | |
return random.random() | |
def measureForever(): | |
while True: | |
# trim old values | |
ts = time.time() - WINDOW | |
while _dq and _dq[0][0] < ts: | |
_dq.popleft() | |
# append new value | |
data = getMeasurement() | |
_dq.append((time.time(), data)) | |
# wake watchers | |
_evt_clr.clear() | |
_evt_set.set() | |
# take a break | |
time.sleep(5) | |
# reset | |
_evt_set.clear() | |
_evt_clr.set() | |
def measurement_sse(id): | |
while True: | |
_evt_set.wait() | |
# just observe the last value in the deque... | |
print 'Got:', id, _dq[-1] | |
_evt_clr.wait() | |
gm = gevent.spawn(measureForever) | |
ga = gevent.spawn(measurement_sse, 'a') | |
gb = gevent.spawn(measurement_sse, 'b') | |
gc = gevent.spawn(measurement_sse, 'c') | |
gevent.joinall([gm, ga, gb, gc]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment