redis实现队列和栈
import scrapy_redis import redis class FifoQueue(object): def __init__(self): self.server = redis.Redis(host='140.143.227.206',port=8888,password='beta') def push(self, request): """Push a request""" self.server.lpush('USERS', request) def pop(self, timeout=0): """Pop a request""" data = self.server.rpop('USERS') return data # [33,22,11] q = FifoQueue() q.push(11) q.push(22) q.push(33) print(q.pop()) print(q.pop()) print(q.pop())
import redis class LifoQueue(object): """Per-spider LIFO queue.""" def __init__(self): self.server = redis.Redis(host='140.143.227.206',port=8888,password='beta') def push(self, request): """Push a request""" self.server.lpush("USERS", request) def pop(self, timeout=0): """Pop a request""" data = self.server.lpop('USERS') return # [33,22,11]
import redis conn = redis.Redis(host='140.143.227.206',port=8888,password='beta') # conn.zadd('score',alex=79, oldboy=33,eric=73) # # print(conn.keys()) v = conn.zrange('score',0,8,desc=True) print(v) pipe = conn.pipeline() pipe.multi() pipe.zrange("score", 0, 0).zremrangebyrank('score', 0, 0) results, count = pipe.execute() print(results,count)
import redis class PriorityQueue(object): """Per-spider priority queue abstraction using redis' sorted set""" def __init__(self): self.server = redis.Redis(host='140.143.227.206',port=8888,password='beta') def push(self, request,score): """Push a request""" # data = self._encode_request(request) # score = -request.priority # We don't use zadd method as the order of arguments change depending on # whether the class is Redis or StrictRedis, and the option of using # kwargs only accepts strings, not bytes. self.server.execute_command('ZADD', 'xxxxxx', score, request) def pop(self, timeout=0): """ Pop a request timeout not support in this queue class """ # use atomic range/remove using multi/exec pipe = self.server.pipeline() pipe.multi() pipe.zrange('xxxxxx', 0, 0).zremrangebyrank('xxxxxx', 0, 0) results, count = pipe.execute() if results: return results[0] q = PriorityQueue() q.push('alex',99) q.push('oldboy',56) q.push('eric',77) v1 = q.pop() print(v1) v2 = q.pop() print(v2) v3 = q.pop() print(v3)
import redis conn = redis.Redis(host='140.143.227.206',port=8888,password='beta') # conn.flushall() print(conn.keys()) # chouti:dupefilter/chouti:request # conn.lpush('xxx:request','http://wwww.xxx.com') # conn.lpush('xxx:request','http://wwww.xxx1.com') # print(conn.lpop('xxx:request')) # print(conn.blpop('xxx:request',timeout=10))
# by luffycity.com import redis conn = redis.Redis(host='140.143.227.206',port=8888,password='beta') conn.lpush('chouti:start_urls','https://dig.chouti.com/r/pic/hot/1')