2020-2-04 13:18作者: 来源:未闻Code
import json import redis import pymongo client = redis.Redis() handler = pymongo.MongoClient().example.col while True: data_raw = client.blpop('data', timeout=300) if not data_raw: continue data = json.loads(data_raw[1].decode()) handler.insert_one(data) |
import json import redis import pymongo client = redis.Redis() handler = pymongo.MongoClient().example.col to_be_insert = [] while True: data_raw = client.blpop('data', timeout=300) if not data_raw: continue data = json.loads(data_raw[1].decode()) to_be_insert.append(data) if len(to_be_insert) >= 1000: handler.insert_many(to_be_insert) to_be_insert = [] |
import signal import json import redis import pymongo client = redis.Redis() handler = pymongo.MongoClient().example.col stop = False def keyboard_handler(signum, frame): global stop stop = True signal.signal(signal.SIGINT, keyboard_handler) to_be_insert = [] while not stop: data_raw = client.blpop('data', timeout=300) if not data_raw: continue data = json.loads(data_raw[1].decode()) to_be_insert.append(data) if len(to_be_insert) >= 1000: handler.insert_many(to_be_insert) to_be_insert = [] if to_be_insert: handler.insert_many(to_be_insert) |
if to_be_insert: handler.insert_many(to_be_insert) |
2021-3-31 09:18
2020-2-03 13:09
2020-1-23 09:34
2020-1-23 09:32
2020-1-23 09:29
2020-1-23 09:26