author | Radek Brich <radek.brich@devl.cz> |
Wed, 26 Sep 2012 16:20:26 +0200 | |
changeset 42 | 9e3775460792 |
parent 41 | 6aad5e35efe8 |
child 43 | a921669e913a |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
# |
|
3 |
# PgManager - manage database connections |
|
4 |
# |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
5 |
# Requires: Python 3.2, psycopg2 |
0 | 6 |
# |
9
2fcc8ef0b97d
Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
7 |
# Part of pgtoolkit |
2fcc8ef0b97d
Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
8 |
# http://hg.devl.cz/pgtoolkit |
2fcc8ef0b97d
Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
9 |
# |
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
10 |
# Copyright (c) 2010, 2011, 2012 Radek Brich <radek.brich@devl.cz> |
0 | 11 |
# |
12 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
13 |
# of this software and associated documentation files (the "Software"), to deal |
|
14 |
# in the Software without restriction, including without limitation the rights |
|
15 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
16 |
# copies of the Software, and to permit persons to whom the Software is |
|
17 |
# furnished to do so, subject to the following conditions: |
|
18 |
# |
|
19 |
# The above copyright notice and this permission notice shall be included in |
|
20 |
# all copies or substantial portions of the Software. |
|
21 |
# |
|
22 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
23 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
24 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
25 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
26 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
27 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
28 |
# THE SOFTWARE. |
|
29 |
||
30 |
"""Postgres database connection manager |
|
31 |
||
32 |
PgManager wraps psycopg2 connect function, adding following features: |
|
33 |
||
34 |
* Manage database connection parameters - link connection parameters |
|
35 |
to an unique identifier, retrieve connection object by this identifier |
|
36 |
||
37 |
* Connection pooling - connections with same identifier are pooled and reused |
|
38 |
||
39 |
* Easy query using the with statement - retrieve cursor directly by connection |
|
40 |
identifier, don't worry about connections |
|
41 |
||
42 |
* Dict rows - cursor has additional methods like fetchall_dict(), which |
|
43 |
returns dict row instead of ordinary list-like row |
|
44 |
||
45 |
Example: |
|
46 |
||
47 |
import pgmanager |
|
48 |
||
49 |
pgm = pgmanager.get_instance() |
|
50 |
pgm.create_conn(hostaddr='127.0.0.1', dbname='postgres') |
|
51 |
||
52 |
with pgm.cursor() as curs: |
|
53 |
curs.execute('SELECT now() AS now') |
|
54 |
row = curs.fetchone_dict() |
|
55 |
print row.now |
|
56 |
||
57 |
First, we have obtained PgManager instance. This is like calling |
|
58 |
PgManager(), although in our example the instance is global. That means |
|
59 |
getting the instance in another module brings us all the defined connections |
|
60 |
etc. |
|
61 |
||
9
2fcc8ef0b97d
Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
62 |
On second line we have created connection named 'default' (this name can be left out). |
0 | 63 |
The with statement obtains connection (actually connects to database when needed), |
9
2fcc8ef0b97d
Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
64 |
then returns cursor for this connection. At the end of with statement, |
2fcc8ef0b97d
Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
65 |
the connection is returned to the pool or closed (depending on number of connections |
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
66 |
in pool and on setting of pool_size parameter). |
0 | 67 |
|
68 |
The row returned by fetchone_dict() is special dict object, which can be accessed |
|
69 |
using item or attribute access, that is row['now'] or row.now. |
|
70 |
""" |
|
71 |
||
72 |
from contextlib import contextmanager |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
73 |
from collections import OrderedDict |
0 | 74 |
import logging |
75 |
import threading |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
76 |
import multiprocessing |
0 | 77 |
import select |
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
78 |
import socket |
0 | 79 |
|
80 |
import psycopg2 |
|
81 |
import psycopg2.extensions |
|
82 |
||
19
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
83 |
from psycopg2 import DatabaseError, IntegrityError, OperationalError |
0 | 84 |
|
85 |
||
26
7f219da7ab71
Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
86 |
log_sql = logging.getLogger("pgmanager_sql") |
7f219da7ab71
Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
87 |
log_notices = logging.getLogger("pgmanager_notices") |
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
88 |
|
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
89 |
|
0 | 90 |
class PgManagerError(Exception): |
91 |
||
92 |
pass |
|
93 |
||
94 |
||
95 |
class ConnectionInfo: |
|
96 |
||
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
97 |
def __init__(self, name, dsn, isolation_level=None, keep_alive=True, |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
98 |
init_statement=None, pool_size=1): |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
99 |
self.name = name # connection name is logged with SQL queries |
42
9e3775460792
PgManager: update comments.
Radek Brich <radek.brich@devl.cz>
parents:
41
diff
changeset
|
100 |
self.dsn = dsn # dsn or string with connection parameters |
0 | 101 |
self.isolation_level = isolation_level |
19
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
102 |
self.keep_alive = keep_alive |
0 | 103 |
self.init_statement = init_statement |
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
104 |
self.pool_size = pool_size |
0 | 105 |
|
106 |
||
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
107 |
class RowDict(OrderedDict): |
0 | 108 |
|
109 |
def __getattr__(self, key): |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
110 |
try: |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
111 |
return self[key] |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
112 |
except KeyError: |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
113 |
raise AttributeError(key) |
0 | 114 |
|
115 |
||
116 |
class Cursor(psycopg2.extensions.cursor): |
|
117 |
||
118 |
def execute(self, query, args=None): |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
119 |
self._log_query(query, args) |
0 | 120 |
try: |
121 |
return super(Cursor, self).execute(query, args) |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
122 |
except DatabaseError: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
123 |
self._log_exception() |
0 | 124 |
|
125 |
def callproc(self, procname, args=None): |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
126 |
self._log_query('CALL %s(%s)' % (procname, ','.join(args))) |
0 | 127 |
try: |
128 |
return super(Cursor, self).callproc(procname, args) |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
129 |
except DatabaseError: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
130 |
self._log_exception() |
0 | 131 |
|
132 |
def row_dict(self, row, lstrip=None): |
|
133 |
adjustname = lambda a: a |
|
134 |
if lstrip: |
|
135 |
adjustname = lambda a: a.lstrip(lstrip) |
|
136 |
return RowDict(zip([adjustname(desc[0]) for desc in self.description], row)) |
|
137 |
||
138 |
def fetchone_dict(self, lstrip=None): |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
139 |
'''Return one row as OrderedDict''' |
0 | 140 |
row = super(Cursor, self).fetchone() |
7
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
141 |
if row is None: |
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
142 |
return None |
0 | 143 |
return self.row_dict(row, lstrip) |
144 |
||
145 |
def fetchall_dict(self, lstrip=None): |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
146 |
'''Return all rows as OrderedDict''' |
0 | 147 |
rows = super(Cursor, self).fetchall() |
148 |
return [self.row_dict(row, lstrip) for row in rows] |
|
149 |
||
41
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
150 |
def adapt(self, row): |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
151 |
if isinstance(row, RowDict): |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
152 |
# dict |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
153 |
adapted = RowDict() |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
154 |
for key in row.keys(): |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
155 |
adapted[key] = self.mogrify('%s', [row[key]]).decode('utf8') |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
156 |
else: |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
157 |
# list |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
158 |
adapted = [self.mogrify('%s', [x]).decode('utf8') for x in row] |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
159 |
return adapted |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
160 |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
161 |
def fetchone_adapted(self, lstrip=None): |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
162 |
'''Like fetchone_dict() but values are quoted for direct inclusion in SQL query. |
7
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
163 |
|
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
164 |
This is useful when you need to generate SQL script from data returned |
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
165 |
by the query. Use mogrify() for simple cases. |
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
166 |
|
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
167 |
''' |
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
168 |
row = super(Cursor, self).fetchone() |
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
169 |
if row is None: |
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
170 |
return None |
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
171 |
return self.row_dict([self.mogrify('%s', [x]).decode('utf8') for x in row], lstrip) |
7
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
172 |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
173 |
def fetchall_adapted(self, lstrip=None): |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
174 |
'''Like fetchall_dict() but values are quoted for direct inclusion in SQL query.''' |
7
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
175 |
rows = super(Cursor, self).fetchall() |
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
26
diff
changeset
|
176 |
return [self.row_dict([self.mogrify('%s', [x]).decode('utf8') for x in row], lstrip) for row in rows] |
7
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
177 |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
178 |
def _log_query(self, query, args=None): |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
179 |
name = self.connection.name if hasattr(self.connection, 'name') else '-' |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
180 |
log_sql.info('[%s] %s' % (name, self.mogrify(query, args).decode('utf8'))) |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
181 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
182 |
def _log_exception(self): |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
183 |
name = self.connection.name if hasattr(self.connection, 'name') else '-' |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
184 |
log_sql.exception('[%s] exception:' % (name,)) |
0 | 185 |
|
186 |
class Connection(psycopg2.extensions.connection): |
|
187 |
||
188 |
def cursor(self, name=None): |
|
189 |
if name is None: |
|
190 |
return super(Connection, self).cursor(cursor_factory=Cursor) |
|
191 |
else: |
|
192 |
return super(Connection, self).cursor(name, cursor_factory=Cursor) |
|
193 |
||
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
194 |
def keep_alive(self): |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
195 |
'''Set socket to keepalive mode. Must be called before any query.''' |
37 | 196 |
sock = socket.fromfd(self.fileno(), socket.AF_INET, socket.SOCK_STREAM) |
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
197 |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) |
19
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
198 |
try: |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
199 |
# Maximum keep-alive probes before asuming the connection is lost |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
200 |
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5) |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
201 |
# Interval (in seconds) between keep-alive probes |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
202 |
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 2) |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
203 |
# Maximum idle time (in seconds) before start sending keep-alive probes |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
204 |
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 10) |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
205 |
except socket.error: |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
206 |
pass |
37 | 207 |
# close duplicated fd, options set for socket stays |
208 |
sock.close() |
|
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
209 |
|
0 | 210 |
|
211 |
class PgManager: |
|
212 |
||
213 |
def __init__(self): |
|
214 |
self.conn_known = {} # available connections |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
215 |
self.conn_pool = {} # active connetions |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
216 |
self.lock = threading.Lock() # mutual exclusion for threads |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
217 |
self.pid = multiprocessing.current_process().pid # forking check |
0 | 218 |
|
219 |
def __del__(self): |
|
220 |
for conn in tuple(self.conn_known.keys()): |
|
221 |
self.destroy_conn(conn) |
|
222 |
||
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
223 |
def create_conn(self, name='default', isolation_level=None, keep_alive=True, init_statement=None, |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
224 |
pool_size=1, dsn=None, **kwargs): |
19
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
225 |
'''Create named connection. |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
226 |
|
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
227 |
name -- name for connection (default is "default") |
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
228 |
pool_size -- how many connections will be kept open in pool |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
229 |
(more connections will still be created but they will be closed by put_conn) |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
230 |
None - disable pool, always return same connection |
19
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
231 |
isolation_level -- "autocommit", "read_committed", "serializable" or None for driver default |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
232 |
keep_alive -- set socket to keepalive mode |
42
9e3775460792
PgManager: update comments.
Radek Brich <radek.brich@devl.cz>
parents:
41
diff
changeset
|
233 |
dsn -- connection string (parameters or data source name) |
19
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
234 |
|
42
9e3775460792
PgManager: update comments.
Radek Brich <radek.brich@devl.cz>
parents:
41
diff
changeset
|
235 |
Other keyword args are used as connection parameters. |
19
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
236 |
|
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
237 |
''' |
0 | 238 |
if name in self.conn_known: |
2 | 239 |
raise PgManagerError('Connection name "%s" already registered.' % name) |
0 | 240 |
|
241 |
if dsn is None: |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
242 |
dsn = ' '.join([x[0]+'='+str(x[1]) for x in kwargs.items() if x[1] is not None]) |
0 | 243 |
|
244 |
isolation_level = self._normalize_isolation_level(isolation_level) |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
245 |
ci = ConnectionInfo(name, dsn, isolation_level, keep_alive, init_statement, pool_size) |
0 | 246 |
|
247 |
self.conn_known[name] = ci |
|
248 |
self.conn_pool[name] = [] |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
249 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
250 |
def create_conn_listen(self, name, channel, dsn=None, copy_dsn=None, **kwargs): |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
251 |
'''Create connection listening for notifies. |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
252 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
253 |
Disables pool. If you want to use pool, create other connection for that. |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
254 |
This connection can be used as usual: conn.cursor() etc. |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
255 |
Don't use PgManager's cursor() and put_conn(). |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
256 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
257 |
name -- name for connection |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
258 |
channel -- listen on this channel |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
259 |
copy_dsn -- specify name of other connection and its dsn will be used |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
260 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
261 |
Other parameters forwarded to create_conn(). |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
262 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
263 |
''' |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
264 |
if dsn is None and copy_dsn: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
265 |
try: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
266 |
dsn = self.conn_known[copy_dsn].dsn |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
267 |
except KeyError: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
268 |
raise PgManagerError("Connection name '%s' not registered." % copy_dsn) |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
269 |
listen_query = "LISTEN " + channel |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
270 |
self.create_conn(name=name, pool_size=None, isolation_level='autocommit', init_statement=listen_query, |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
271 |
dsn=dsn, **kwargs) |
0 | 272 |
|
273 |
def close_conn(self, name='default'): |
|
274 |
'''Close all connections of given name. |
|
275 |
||
276 |
Connection credentials are still saved. |
|
277 |
||
278 |
''' |
|
279 |
while len(self.conn_pool[name]): |
|
280 |
conn = self.conn_pool[name].pop() |
|
281 |
conn.close() |
|
282 |
||
283 |
def destroy_conn(self, name='default'): |
|
284 |
'''Destroy connection. |
|
285 |
||
286 |
Counterpart of create_conn. |
|
287 |
||
288 |
''' |
|
289 |
if not name in self.conn_known: |
|
2 | 290 |
raise PgManagerError('Connection name "%s" not registered.' % name) |
0 | 291 |
|
292 |
self.close_conn(name) |
|
293 |
||
294 |
del self.conn_known[name] |
|
295 |
del self.conn_pool[name] |
|
296 |
||
297 |
def get_conn(self, name='default'): |
|
298 |
'''Get connection of name 'name' from pool.''' |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
299 |
self._check_fork() |
0 | 300 |
self.lock.acquire() |
301 |
try: |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
302 |
try: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
303 |
ci = self.conn_known[name] |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
304 |
except KeyError: |
2 | 305 |
raise PgManagerError("Connection name '%s' not registered." % name) |
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
306 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
307 |
# no pool, just one static connection |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
308 |
if ci.pool_size is None: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
309 |
# check for existing connection |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
310 |
try: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
311 |
conn = self.conn_pool[name][0] |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
312 |
if conn.closed: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
313 |
conn = None |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
314 |
except IndexError: |
0 | 315 |
conn = None |
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
316 |
self.conn_pool[name].append(conn) |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
317 |
# if no existing connection is valid, connect new one and save it |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
318 |
if conn is None: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
319 |
conn = self._connect(ci) |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
320 |
self.conn_pool[name][0] = conn |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
321 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
322 |
# connection from pool |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
323 |
else: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
324 |
conn = None |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
325 |
while len(self.conn_pool[name]) and conn is None: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
326 |
conn = self.conn_pool[name].pop() |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
327 |
if conn.closed: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
328 |
conn = None |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
329 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
330 |
if conn is None: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
331 |
conn = self._connect(ci) |
0 | 332 |
finally: |
333 |
self.lock.release() |
|
334 |
return conn |
|
335 |
||
336 |
def put_conn(self, conn, name='default'): |
|
337 |
'''Put connection back to pool. |
|
338 |
||
339 |
Name must be same as used for get_conn, |
|
340 |
otherwise things become broken. |
|
341 |
||
342 |
''' |
|
343 |
self.lock.acquire() |
|
344 |
try: |
|
345 |
if not name in self.conn_known: |
|
2 | 346 |
raise PgManagerError("Connection name '%s' not registered." % name) |
0 | 347 |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
348 |
if len(self.conn_pool[name]) >= self.conn_known[name].pool_size: |
0 | 349 |
conn.close() |
350 |
return |
|
351 |
||
352 |
if conn.get_transaction_status() == psycopg2.extensions.TRANSACTION_STATUS_UNKNOWN: |
|
353 |
conn.close() |
|
354 |
return |
|
355 |
||
356 |
# connection returned to the pool must not be in transaction |
|
357 |
if conn.get_transaction_status() != psycopg2.extensions.TRANSACTION_STATUS_IDLE: |
|
33
bd0beda49bcb
PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
358 |
try: |
bd0beda49bcb
PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
359 |
conn.rollback() |
bd0beda49bcb
PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
360 |
except OperationalError: |
bd0beda49bcb
PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
361 |
if not conn.closed: |
bd0beda49bcb
PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
362 |
conn.close() |
bd0beda49bcb
PgManager: log connection name with queries. BatchUpdateTool: handle some possible exceptions and try reconnect to database.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
363 |
return |
0 | 364 |
|
365 |
self.conn_pool[name].append(conn) |
|
366 |
finally: |
|
367 |
self.lock.release() |
|
368 |
||
369 |
@contextmanager |
|
370 |
def cursor(self, name='default'): |
|
371 |
'''Cursor context. |
|
372 |
||
373 |
Uses any connection of name 'name' from pool |
|
374 |
and returns cursor for that connection. |
|
375 |
||
376 |
''' |
|
377 |
conn = self.get_conn(name) |
|
378 |
||
379 |
try: |
|
380 |
curs = conn.cursor() |
|
381 |
yield curs |
|
382 |
finally: |
|
383 |
curs.close() |
|
26
7f219da7ab71
Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
384 |
self.log_notices(conn) |
0 | 385 |
self.put_conn(conn, name) |
386 |
||
26
7f219da7ab71
Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
387 |
def log_notices(self, conn): |
7f219da7ab71
Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
388 |
for notice in conn.notices: |
7f219da7ab71
Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
389 |
log_notices.info(notice.rstrip()) |
7f219da7ab71
Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
390 |
conn.notices[:] = [] |
7f219da7ab71
Add logging mechanism for notices from postgres server. Rename SQL log to "pgmanager_sql".
Radek Brich <radek.brich@devl.cz>
parents:
24
diff
changeset
|
391 |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
392 |
def wait_for_notify(self, name='default', timeout=5.0): |
0 | 393 |
'''Wait for asynchronous notifies, return the last one. |
394 |
||
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
395 |
name -- name of connection, must be created using create_conn_listen() |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
396 |
timeout -- in seconds, floating point (None - wait forever) |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
397 |
|
0 | 398 |
Returns None on timeout. |
399 |
||
400 |
''' |
|
401 |
conn = self.get_conn(name) |
|
402 |
||
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
403 |
# return any notifies on stack |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
404 |
if conn.notifies: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
405 |
return conn.notifies.pop() |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
406 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
407 |
if select.select([conn], [], [], timeout) == ([], [], []): |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
408 |
# timeout |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
409 |
return None |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
410 |
else: |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
411 |
conn.poll() |
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
412 |
|
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
413 |
# return just the last notify (we do not care for older ones) |
0 | 414 |
if conn.notifies: |
415 |
return conn.notifies.pop() |
|
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
416 |
return None |
0 | 417 |
|
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
418 |
def _connect(self, ci): |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
419 |
conn = psycopg2.connect(ci.dsn, connection_factory=Connection) |
36
e67101c22e83
pgmanager: Add create_conn_listen() which should be used with wait_for_notify. Update wait_for_notify() to not use put_conn(). Add name to ConnectionInfo. Log queries before they are called. Log exceptions. Add notifyexample.
Radek Brich <radek.brich@devl.cz>
parents:
33
diff
changeset
|
420 |
conn.name = ci.name |
19
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
421 |
if ci.keep_alive: |
e526ca146fa9
Add documentation for create_conn(). Fix keep_alive - do not crash if socket settings are not supported.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
422 |
conn.keep_alive() |
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
423 |
if not ci.isolation_level is None: |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
424 |
conn.set_isolation_level(ci.isolation_level) |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
425 |
if ci.init_statement: |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
426 |
curs = conn.cursor() |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
427 |
curs.execute(ci.init_statement) |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
428 |
curs.close() |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
429 |
return conn |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
430 |
|
0 | 431 |
def _normalize_isolation_level(self, level): |
432 |
if type(level) == str: |
|
433 |
if level.lower() == 'autocommit': |
|
434 |
return psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT |
|
435 |
if level.lower() == 'read_committed': |
|
436 |
return psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED |
|
437 |
if level.lower() == 'serializable': |
|
438 |
return psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE |
|
439 |
raise PgManagerError('Unknown isolation level name: "%s"', level) |
|
440 |
return level |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
441 |
|
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
442 |
def _check_fork(self): |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
443 |
'''Check if process was forked (PID has changed). |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
444 |
|
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
445 |
If it was, clean parent's connections. |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
446 |
New connections are created for children. |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
447 |
Known connection credentials are inherited, but not shared. |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
448 |
|
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
449 |
''' |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
450 |
if self.pid == multiprocessing.current_process().pid: |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
451 |
# PID has not changed |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
452 |
return |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
453 |
|
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
454 |
# update saved PID |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
455 |
self.pid = multiprocessing.current_process().pid |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
456 |
# reinitialize lock |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
457 |
self.lock = threading.Lock() |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
458 |
# clean parent's connections |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
459 |
for name in self.conn_pool: |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
460 |
self.conn_pool[name] = [] |
0 | 461 |
|
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
462 |
@classmethod |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
463 |
def get_instance(cls): |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
464 |
if not hasattr(cls, '_instance'): |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
465 |
cls._instance = cls() |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
466 |
return cls._instance |
0 | 467 |
|
468 |
||
469 |
def get_instance(): |
|
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
19
diff
changeset
|
470 |
return PgManager.get_instance() |
0 | 471 |