Skip to content

Instantly share code, notes, and snippets.

@mattbillenstein
Last active May 23, 2016 12:08
Show Gist options
  • Save mattbillenstein/7baa1d0d006cf6981942 to your computer and use it in GitHub Desktop.
Save mattbillenstein/7baa1d0d006cf6981942 to your computer and use it in GitHub Desktop.
gevent streamer thingy
#!/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