pymongo

"""
curd
"""
import os

import pymongo
from typing import Dict, List, Tuple
from bson import ObjectId

from App import config


class MongoConnectDao:

    def __init__(self, mongo_url: str = config.MONGODB_URL):
        if not mongo_url:
            try:
                mongo_url = os.environ['MONGODB_URL']
            except:
                pass
        collect_db = "YCSpider"
        collect_name = 'realTimeCrawl'
        mongodb = pymongo.MongoClient(mongo_url, maxPoolSize=200)[collect_db]
        self.mongodb = mongodb
        self.collect = mongodb[collect_name]

    def __call__(self, *args, **kwargs):
        pass

    def __new__(cls):
        if not hasattr(cls, 'instance'):
            print("not instance")
            cls.instance = super(MongoConnectDao, cls).__new__(cls)
        return cls.instance

    def insert_one(self, item: Dict) -> ObjectId:
        return self.collect.insert_one(item).inserted_id

    def insert_many(self, item: List[Dict]) -> List[ObjectId]:
        return self.collect.insert_many(item).inserted_ids

    def find_all(self, item: Dict, limit: int = 0, skip: int = 0) -> List[Dict]:
        return [doc for doc in self.collect.find(item).limit(limit).skip(skip)]

    def find_one(self, item: Dict) -> Dict:
        return self.collect.find_one(item)

    def update(self, item: Dict):
        pass

    def count(self, item: Dict) -> int:
        return self.collect.count_documents(item)

    def close(self):
        """
        当close()被任何线程调用时,所有空闲套接字都会被关闭,并且所有正在使用的套接字在返回到池中时也会被关闭。
        :return:
        """
        self.mongodb.client.close()


if __name__ == '__main__':
    # from concurrent.futures import ThreadPoolExecutor
    #
    # pool = ThreadPoolExecutor(max_workers=200)
    #
    #
    # def exe_mongo():
    #     mb = MongoConnectDao()
    #     print(mb.count({}))
    #     print(mb.find_all({}, limit=10))
    #
    #
    # def run(pool):
    #     [pool.submit(exe_mongo) for i in range(301)]
    #     pool.shutdown(wait=True)
    #
    #
    # run(pool)
    pass

你可能感兴趣的:(python)