motor的使用

# -*- coding: utf-8 -*-
# @Time : 2018/11/18 10:41 PM
# @Author : cxa
# @File : motordb.py
# @Software: PyCharm
import asyncio

try:
    import uvloop

    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
    pass

from motor.motor_asyncio import AsyncIOMotorClient
from pymongo import UpdateOne


class MotorBase:
    _db = {}
    _collection = {}

    def __init__(self, loop=None):
        self.motor_uri = ''
        self.loop = loop or asyncio.get_event_loop()

    def client(self, db):
        self.motor_uri = f"mongodb://localhost:27017/{db}"
        return AsyncIOMotorClient(self.motor_uri, io_loop=self.loop)

    def get_db(self, db='test'):
        if db not in self._db:
            self._db[db] = self.client(db)[db]

        return self._db[db]


async def savedata():
    mb = MotorBase().get_db('test')
    await mb.news.insert_one({'name': "lisa"})

async def save_data(items, col="demo", key="obj_id"):
    """
    :param items:
    :param col:
    :param key:
    :return:
    """
    # storage.info(f"此时的items:{items}")
    # UpdateOne
    mb = MotorBase().get_db("aio_spider_data")
    if isinstance(items, list):
        requests = list()
        r_a = requests.append
        for item in items:
            try:
                r_a(UpdateOne({
                    key: item.get(key)},
                    {'$set': item},
                    upsert=True))
            except Exception as e:
                storage.error(f"数据插入出错:{e.args}此时的item是:{item}")
        await mb[col].bulk_write(requests, ordered=False, bypass_document_validation=True)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(savedata())

posted @ 2018-11-18 23:15  公众号python学习开发  阅读(1567)  评论(0编辑  收藏  举报