-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
50 lines (41 loc) · 1.91 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import multiprocessing
import sys
import os
from pymongo.errors import ConnectionFailure
from bson.objectid import ObjectId
from multiprocessing import Pool
from pymongo import MongoClient
from loguru import logger
from time import time
def set_logger(logger_):
global logger
logger = logger_
def multiple_mdb_connection(stck_id):
client=MongoClient('mongodb://travis:test@127.0.0.1:27017/mydb')
db=client['mydb']
collection = db['contacts']
logger.opt(colors=True).info(f'{multiprocessing.current_process().name} <c>///</c> {collection.find_one({"_id": ObjectId(stck_id)})}')
if __name__ == "__main__":
logger.remove()
logger.add(sys.stdout, level='DEBUG', format="<g>{time:YYYY-MM-DD HH:mm:ss}</g> | <m>{level}</m> | {message}", enqueue=True)
begin_time_general_process = time()
try:
client=MongoClient('mongodb://travis:test@127.0.0.1:27017/mydb')
logger.opt(colors=True).debug(f'<c>{client.list_database_names()}</c>')
db=client['mydb']
collection = db['contacts']
for idx in range(10000):
collection.insert_one({"name":"Mr.Geek","eid":idx})
cursor=collection.find().distinct('_id')
logger.opt(colors=True).info(f'<g>Number of documents {collection.count_documents({})}</g>')
stack = [record for record in cursor]
with Pool(processes=8, initializer=set_logger, initargs=(logger,)) as pool:
pool.map(multiple_mdb_connection, stack)
except:
logger.opt(colors=True).debug(f'<y>No connection</y>')
end_time_general_process = time()
logger.opt(colors=True).debug('Processing time: <c>{:02d}:{:02d}:{:02d}</c>'.format(
int(end_time_general_process - begin_time_general_process) // 3600,
(int(end_time_general_process - begin_time_general_process) % 3600 // 60),
int(end_time_general_process - begin_time_general_process) % 60))
logger.info("Done")