实在无聊就写了个很小的python程序用来实现模拟redis队列的代码如下:
redis_lpush.py
#!/usr/bin/python3
import time
import redis
def handle(info):
print(info)
time.sleep(2)
def main():
pool = redis.ConnectionPool(host=‘localhost‘, port=6379, db=0)
r = redis.Redis(connection_pool=pool)
i = 0
while i < 30:
i += 1
r.lpush(‘task‘, i)
handle(i)
if __name__ == "__main__":
main()
redis_brpop.py
#!/usr/bin/python3
import time
import redis
def handle(info):
print(info)
time.sleep(2)
def main():
pool = redis.ConnectionPool(host=‘localhost‘, port=6379, db=0)
r = redis.Redis(connection_pool=pool)
while 1:
result = r.brpop(‘task‘, 0)
handle(result[1])
if __name__ == "__main__":
main()
最终效果如下图:
有跟我一样的人么——放假前半天不知道干什么,有的话举个手~~~
时间: 2024-10-09 10:56:39