Created
December 30, 2013 14:28
-
-
Save wolf0403/8182670 to your computer and use it in GitHub Desktop.
Worker model with Redis and Gevent
This file contains hidden or 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 | |
from __future__ import print_function | |
import gevent | |
import gevent.monkey | |
gevent.monkey.patch_all() | |
import gevent.hub | |
import redis | |
Q = 'testqueue' | |
def init_sink(): | |
r = redis.StrictRedis() | |
return r | |
import time | |
inittime = None | |
class Worker(object): | |
def __init__(self, worker_id, r, proc=print): | |
self._worker_id = worker_id | |
self._proc = proc | |
self._r = r | |
def run(self): | |
print('worker {} started'.format(self._worker_id)) | |
while True: | |
v = self._r.blpop(Q, timeout=1) | |
if v: | |
self._proc((self._worker_id, v)) | |
gevent.sleep(1.5) | |
else: | |
print(self._worker_id, ' sleeping: ', (time.time() - inittime)) | |
gevent.sleep(1) | |
def init_loop(worker_n=5): | |
redisq = init_sink() | |
workers = [None]*worker_n | |
global inittime | |
inittime = time.time() | |
for _id in range(worker_n): | |
print('Creating ', _id) | |
fun = Worker(_id, redisq).run | |
workers[_id] = gevent.spawn(fun) | |
#w.start() | |
gevent.hub.get_hub().switch() | |
return workers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment