现在,我的mongodb有一个表,其中是关键字和关键字的抓取数量,现在如何将关键字插入redis列表和抓取优先级数? THKS!非常 这是我的代码:
def init_mongo_to_redis(mongo_db, redis_pool):
r = redis.Redis(connection_pool = redis_pool)
mongo_handle = mongo_db.keywords_tbl.find({}, {'keyword': 1, 'keyword_type': 1, \
'ignore_station': 1}, no_cursor_timeout=True)
redis_len = r.llen('fetch_keywords')
if redis_len != 0:
logging.info('redis fetch_keywords size is %d', redis_len)
return
logging.info('init redis fetch_keywords start')
r_pipe = r.pipeline()
pbar = tqdm(mongo_handle)
for keyword in pbar:
item = {
'keyword_type': keyword['keyword_type'],
'ignore_station': keyword['ignore_station'],
'keyword': keyword['keyword']
}
r_pipe.lpush('fetch_keywords', json.dumps(item))
pbar.set_description('Processing %s' % keyword)
r_pipe.execute()
logging.info('init redis fetch_keywords end :%d', r.llen('fetch_keywords'))