思路还是挺简单的,以列表或者其他容器作为搭载,将需要传输的参数写入容器,再将这个容器push到队列中。取参数的时候将容器里的内容对应取出即可,以下为示例代码:
将参数push进Redis队列:
r = redis.Redis(host=REDISHOST, port=REDISPORT, db=REDISDB)
r.rpush("test:items", [param1, param2])
从Redis队列拿到参数:
r = redis.Redis(host=config.REDISHOST, port=config.REDISPORT, db=config.REDISDB)
while True:
try:
s, item = r.blpop(["test:items"], timeout=30)
except:
print ('30s内没有队列进来,终止程序。')
break
item = json.loads(item.decode('utf8')
# param1和param2即为容器中要拿到的参数
param1 = item[0]
param2 = item[1]