forked from starenka/chopchop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongopool.py
More file actions
38 lines (30 loc) · 1.13 KB
/
mongopool.py
File metadata and controls
38 lines (30 loc) · 1.13 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from threading import currentThread
from mongokit import Connection
import settings
class ConnectionPool(object):
def __init__(self):
super(ConnectionPool, self).__init__()
self.pool = {}
def make_connection(self):
con = Connection(settings.MONGODB_HOST, settings.MONGODB_PORT)
db = con[settings.MONGODB_NAME]
if settings.MONGODB_USER and settings.MONGODB_PASSWORD:
auth = db.authenticate(settings.MONGODB_USER, settings.MONGODB_PASSWORD)
if not auth:
raise AssertionError('Failed to auth to %s:%s/%s as %s' % (
settings.MONGODB_HOST,
settings.MONGODB_PORT,
settings.MONGODB_NAME,
settings.MONGODB_USER)
)
db = db[settings.MONGODB_TABLE]
return db
def get_connection(self):
if currentThread() not in self.pool:
self.pool[currentThread()] = self.make_connection()
return self.pool[currentThread()]
@property
def con(self):
return self.get_connection()