author | Radek Brich <radek.brich@devl.cz> |
Mon, 04 Mar 2013 15:39:34 +0100 | |
changeset 70 | 77e65040711c |
parent 55 | adc1615d8fc5 |
child 71 | 4251068a251a |
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 |
||
70 | 34 |
* Save and reuse database connection parameters |
0 | 35 |
|
70 | 36 |
* Connection pooling |
0 | 37 |
|
70 | 38 |
* Easy query using the with statement |
0 | 39 |
|
70 | 40 |
* Dictionary rows |
41 |
||
42 |
Example usage: |
|
0 | 43 |
|
70 | 44 |
import pgmanager |
0 | 45 |
|
70 | 46 |
pgm = pgmanager.get_instance() |
47 |
pgm.create_conn(hostaddr='127.0.0.1', dbname='postgres') |
|
0 | 48 |
|
70 | 49 |
with pgm.cursor() as curs: |
50 |
curs.execute('SELECT now() AS now') |
|
51 |
row = curs.fetchone_dict() |
|
52 |
print(row.now) |
|
0 | 53 |
|
54 |
First, we have obtained PgManager instance. This is like calling |
|
55 |
PgManager(), although in our example the instance is global. That means |
|
56 |
getting the instance in another module brings us all the defined connections |
|
57 |
etc. |
|
58 |
||
9
2fcc8ef0b97d
Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents:
8
diff
changeset
|
59 |
On second line we have created connection named 'default' (this name can be left out). |
0 | 60 |
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
|
61 |
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
|
62 |
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
|
63 |
in pool and on setting of pool_size parameter). |
0 | 64 |
|
65 |
The row returned by fetchone_dict() is special dict object, which can be accessed |
|
66 |
using item or attribute access, that is row['now'] or row.now. |
|
70 | 67 |
|
0 | 68 |
""" |
69 |
||
70 |
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
|
71 |
from collections import OrderedDict |
0 | 72 |
import logging |
73 |
import threading |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
74 |
import multiprocessing |
0 | 75 |
import select |
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
76 |
import socket |
0 | 77 |
|
78 |
import psycopg2 |
|
79 |
import psycopg2.extensions |
|
80 |
||
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
|
81 |
from psycopg2 import DatabaseError, IntegrityError, OperationalError |
0 | 82 |
|
83 |
||
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
|
84 |
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
|
85 |
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
|
86 |
|
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
|
87 |
|
0 | 88 |
class PgManagerError(Exception): |
89 |
||
90 |
pass |
|
91 |
||
92 |
||
93 |
class ConnectionInfo: |
|
94 |
||
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
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
self.dsn = dsn # dsn or string with connection parameters |
0 | 99 |
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
|
100 |
self.keep_alive = keep_alive |
0 | 101 |
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
|
102 |
self.pool_size = pool_size |
0 | 103 |
|
104 |
||
45 | 105 |
class RowDict: |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
106 |
"""Special read-only dictionary used for rows returned from queries. |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
107 |
|
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
108 |
Initialization is same as for dict: |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
109 |
row = RowDict([('id', 123), ('name', 'hello')]) |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
110 |
|
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
111 |
Allows key and attribute access to contained items: |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
112 |
row['id'] |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
113 |
row.id |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
114 |
|
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
115 |
Items keep order in which columns where returned from database. |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
116 |
|
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
117 |
Tuple style access is also supported: |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
118 |
row[0] |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
119 |
id, name = row |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
120 |
|
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
121 |
""" |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
122 |
|
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
123 |
def __init__(self, data): |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
124 |
self._dict = OrderedDict(data) |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
125 |
|
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
126 |
def __getitem__(self, key): |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
127 |
if isinstance(key, int): |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
128 |
return tuple(self._dict.values())[key] |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
129 |
return self._dict[key] |
0 | 130 |
|
131 |
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
|
132 |
try: |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
133 |
return self._dict[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
|
134 |
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
|
135 |
raise AttributeError(key) |
0 | 136 |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
137 |
def __contains__(self, key): |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
138 |
return key in self._dict |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
139 |
|
48
b82c7c2fb5af
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
140 |
def keys(self): |
b82c7c2fb5af
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
141 |
return self._dict.keys() |
b82c7c2fb5af
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
142 |
|
54 | 143 |
def values(self): |
144 |
return self._dict.values() |
|
145 |
||
55 | 146 |
def items(self): |
147 |
return self._dict.items() |
|
148 |
||
0 | 149 |
|
150 |
class Cursor(psycopg2.extensions.cursor): |
|
151 |
||
152 |
def execute(self, query, args=None): |
|
48
b82c7c2fb5af
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
153 |
# log query before executing |
b82c7c2fb5af
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
154 |
self._log_query(query, args) |
0 | 155 |
try: |
156 |
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
|
157 |
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
|
158 |
self._log_exception() |
46 | 159 |
raise |
0 | 160 |
|
161 |
def callproc(self, procname, args=None): |
|
48
b82c7c2fb5af
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
162 |
# log query before executing (not query actually executed but should correspond) |
b82c7c2fb5af
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
163 |
self._log_query(self._build_callproc_query(procname, len(args)), args) |
0 | 164 |
try: |
165 |
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
|
166 |
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
|
167 |
self._log_exception() |
46 | 168 |
raise |
0 | 169 |
|
170 |
def row_dict(self, row, lstrip=None): |
|
171 |
adjustname = lambda a: a |
|
172 |
if lstrip: |
|
173 |
adjustname = lambda a: a.lstrip(lstrip) |
|
174 |
return RowDict(zip([adjustname(desc[0]) for desc in self.description], row)) |
|
175 |
||
176 |
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
|
177 |
'''Return one row as OrderedDict''' |
0 | 178 |
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
|
179 |
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
|
180 |
return None |
0 | 181 |
return self.row_dict(row, lstrip) |
182 |
||
183 |
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
|
184 |
'''Return all rows as OrderedDict''' |
0 | 185 |
rows = super(Cursor, self).fetchall() |
186 |
return [self.row_dict(row, lstrip) for row in rows] |
|
187 |
||
41
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
188 |
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
|
189 |
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
|
190 |
# dict |
54 | 191 |
adapted = dict() |
41
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
192 |
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
|
193 |
adapted[key] = self.mogrify('%s', [row[key]]).decode('utf8') |
54 | 194 |
return RowDict(adapted) |
41
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
195 |
else: |
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
196 |
# list |
54 | 197 |
return [self.mogrify('%s', [x]).decode('utf8') for x in row] |
41
6aad5e35efe8
PgDataDiff: Fix sorting - do not adapt primary key before sort condition.
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
198 |
|
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
|
199 |
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
|
200 |
'''Like fetchone_dict() but values are quoted for direct inclusion in SQL query. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
201 |
|
7
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
202 |
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
|
203 |
by the query. Use mogrify() for simple cases. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
204 |
|
7
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
205 |
''' |
685b20d2d3ab
Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents:
4
diff
changeset
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
|
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
|
211 |
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
|
212 |
'''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
|
213 |
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
|
214 |
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
|
215 |
|
46 | 216 |
def _log_query(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
|
217 |
name = self.connection.name if hasattr(self.connection, 'name') else '-' |
46 | 218 |
query = self.query or self.mogrify(query, args) |
219 |
log_sql.info('[%s] %s' % (name, query.decode('utf8'))) |
|
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
|
220 |
|
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
|
221 |
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
|
222 |
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
|
223 |
log_sql.exception('[%s] exception:' % (name,)) |
0 | 224 |
|
46 | 225 |
def _build_callproc_query(self, procname, num_args): |
48
b82c7c2fb5af
PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
Radek Brich <radek.brich@devl.cz>
parents:
46
diff
changeset
|
226 |
return 'SELECT * FROM %s(%s)' % (procname, ', '.join(['%s'] * num_args)) |
46 | 227 |
|
228 |
||
0 | 229 |
class Connection(psycopg2.extensions.connection): |
230 |
||
231 |
def cursor(self, name=None): |
|
232 |
if name is None: |
|
233 |
return super(Connection, self).cursor(cursor_factory=Cursor) |
|
234 |
else: |
|
235 |
return super(Connection, self).cursor(name, cursor_factory=Cursor) |
|
236 |
||
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
237 |
def keep_alive(self): |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
238 |
'''Set socket to keepalive mode. Must be called before any query.''' |
37 | 239 |
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
|
240 |
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
|
241 |
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
|
242 |
# 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
|
243 |
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
|
244 |
# 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
|
245 |
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
|
246 |
# 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
|
247 |
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
|
248 |
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
|
249 |
pass |
37 | 250 |
# close duplicated fd, options set for socket stays |
251 |
sock.close() |
|
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
252 |
|
0 | 253 |
|
254 |
class PgManager: |
|
255 |
||
256 |
def __init__(self): |
|
257 |
self.conn_known = {} # available connections |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
258 |
self.conn_pool = {} # active connetions |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
259 |
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
|
260 |
self.pid = multiprocessing.current_process().pid # forking check |
0 | 261 |
|
262 |
def __del__(self): |
|
263 |
for conn in tuple(self.conn_known.keys()): |
|
264 |
self.destroy_conn(conn) |
|
265 |
||
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
|
266 |
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
|
267 |
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
|
268 |
'''Create named connection. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
269 |
|
70 | 270 |
*name* -- name for connection |
271 |
||
272 |
*pool_size* -- how many connections will be kept open in pool. |
|
273 |
More connections will still be created but they will be closed by put_conn. |
|
274 |
`None` will disable pool, get_conn() will then always return same connection. |
|
275 |
||
276 |
*isolation_level* -- `"autocommit"`, `"read_committed"`, `"serializable"` or `None` for driver default |
|
277 |
||
278 |
*keep_alive* -- set socket to keepalive mode |
|
279 |
||
280 |
*dsn* -- connection string (parameters or data source name) |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
281 |
|
42
9e3775460792
PgManager: update comments.
Radek Brich <radek.brich@devl.cz>
parents:
41
diff
changeset
|
282 |
Other keyword args are used as connection parameters. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
283 |
|
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
|
284 |
''' |
0 | 285 |
if name in self.conn_known: |
2 | 286 |
raise PgManagerError('Connection name "%s" already registered.' % name) |
0 | 287 |
|
288 |
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
|
289 |
dsn = ' '.join([x[0]+'='+str(x[1]) for x in kwargs.items() if x[1] is not None]) |
0 | 290 |
|
291 |
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
|
292 |
ci = ConnectionInfo(name, dsn, isolation_level, keep_alive, init_statement, pool_size) |
0 | 293 |
|
294 |
self.conn_known[name] = ci |
|
295 |
self.conn_pool[name] = [] |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
296 |
|
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
|
297 |
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
|
298 |
'''Create connection listening for notifies. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
299 |
|
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
|
300 |
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
|
301 |
This connection can be used as usual: conn.cursor() etc. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
302 |
Don't use PgManager's cursor() and put_conn(). |
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
303 |
|
70 | 304 |
*name* -- name for connection |
305 |
||
306 |
*channel* -- listen on this channel |
|
307 |
||
308 |
*copy_dsn* -- specify name of other connection and its dsn will be used |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
309 |
|
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
|
310 |
Other parameters forwarded to create_conn(). |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
311 |
|
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
|
312 |
''' |
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 |
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
|
314 |
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
|
315 |
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
|
316 |
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
|
317 |
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
|
318 |
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
|
319 |
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
|
320 |
dsn=dsn, **kwargs) |
0 | 321 |
|
322 |
def close_conn(self, name='default'): |
|
323 |
'''Close all connections of given name. |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
324 |
|
0 | 325 |
Connection credentials are still saved. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
326 |
|
0 | 327 |
''' |
328 |
while len(self.conn_pool[name]): |
|
329 |
conn = self.conn_pool[name].pop() |
|
330 |
conn.close() |
|
331 |
||
332 |
def destroy_conn(self, name='default'): |
|
333 |
'''Destroy connection. |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
334 |
|
0 | 335 |
Counterpart of create_conn. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
336 |
|
0 | 337 |
''' |
338 |
if not name in self.conn_known: |
|
2 | 339 |
raise PgManagerError('Connection name "%s" not registered.' % name) |
0 | 340 |
|
341 |
self.close_conn(name) |
|
342 |
||
343 |
del self.conn_known[name] |
|
344 |
del self.conn_pool[name] |
|
345 |
||
346 |
def get_conn(self, name='default'): |
|
347 |
'''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
|
348 |
self._check_fork() |
0 | 349 |
self.lock.acquire() |
350 |
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
|
351 |
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
|
352 |
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
|
353 |
except KeyError: |
2 | 354 |
raise PgManagerError("Connection name '%s' not registered." % name) |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
355 |
|
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
|
356 |
# 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
|
357 |
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
|
358 |
# 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
|
359 |
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
|
360 |
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
|
361 |
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
|
362 |
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
|
363 |
except IndexError: |
0 | 364 |
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
|
365 |
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
|
366 |
# 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
|
367 |
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
|
368 |
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
|
369 |
self.conn_pool[name][0] = conn |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
370 |
|
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
|
371 |
# 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
|
372 |
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
|
373 |
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
|
374 |
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
|
375 |
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
|
376 |
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
|
377 |
conn = None |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
378 |
|
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
|
379 |
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
|
380 |
conn = self._connect(ci) |
0 | 381 |
finally: |
382 |
self.lock.release() |
|
383 |
return conn |
|
384 |
||
385 |
def put_conn(self, conn, name='default'): |
|
386 |
'''Put connection back to pool. |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
387 |
|
70 | 388 |
*name* must be same as used for get_conn, otherwise things become broken. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
389 |
|
0 | 390 |
''' |
391 |
self.lock.acquire() |
|
392 |
try: |
|
393 |
if not name in self.conn_known: |
|
2 | 394 |
raise PgManagerError("Connection name '%s' not registered." % name) |
0 | 395 |
|
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
|
396 |
if len(self.conn_pool[name]) >= self.conn_known[name].pool_size: |
0 | 397 |
conn.close() |
398 |
return |
|
399 |
||
400 |
if conn.get_transaction_status() == psycopg2.extensions.TRANSACTION_STATUS_UNKNOWN: |
|
401 |
conn.close() |
|
402 |
return |
|
403 |
||
404 |
# connection returned to the pool must not be in transaction |
|
405 |
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
|
406 |
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
|
407 |
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
|
408 |
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
|
409 |
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
|
410 |
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
|
411 |
return |
0 | 412 |
|
413 |
self.conn_pool[name].append(conn) |
|
414 |
finally: |
|
415 |
self.lock.release() |
|
416 |
||
417 |
@contextmanager |
|
418 |
def cursor(self, name='default'): |
|
419 |
'''Cursor context. |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
420 |
|
70 | 421 |
Uses any connection info with *name* from pool |
0 | 422 |
and returns cursor for that connection. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
423 |
|
0 | 424 |
''' |
425 |
conn = self.get_conn(name) |
|
426 |
||
427 |
try: |
|
428 |
curs = conn.cursor() |
|
429 |
yield curs |
|
430 |
finally: |
|
431 |
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
|
432 |
self.log_notices(conn) |
0 | 433 |
self.put_conn(conn, name) |
434 |
||
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
|
435 |
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
|
436 |
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
|
437 |
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
|
438 |
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
|
439 |
|
70 | 440 |
def wait_for_notify(self, name='default', timeout=None): |
0 | 441 |
'''Wait for asynchronous notifies, return the last one. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
442 |
|
70 | 443 |
*name* -- name of connection, must be created using `create_conn_listen()` |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
444 |
|
70 | 445 |
*timeout* -- in seconds, floating point (`None` means wait forever) |
446 |
||
447 |
Returns `None` on timeout. |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
448 |
|
0 | 449 |
''' |
450 |
conn = self.get_conn(name) |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
451 |
|
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
|
452 |
# 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
|
453 |
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
|
454 |
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
|
455 |
|
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
|
456 |
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
|
457 |
# 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
|
458 |
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
|
459 |
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
|
460 |
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
|
461 |
|
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
|
462 |
# return just the last notify (we do not care for older ones) |
0 | 463 |
if conn.notifies: |
464 |
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
|
465 |
return None |
0 | 466 |
|
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
467 |
def _connect(self, ci): |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
468 |
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
|
469 |
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
|
470 |
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
|
471 |
conn.keep_alive() |
8
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
472 |
if not ci.isolation_level is None: |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
473 |
conn.set_isolation_level(ci.isolation_level) |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
474 |
if ci.init_statement: |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
475 |
curs = conn.cursor() |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
476 |
curs.execute(ci.init_statement) |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
477 |
curs.close() |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
478 |
return conn |
2911935c524d
pgmanager: Add keep_alive support.
Radek Brich <radek.brich@devl.cz>
parents:
7
diff
changeset
|
479 |
|
0 | 480 |
def _normalize_isolation_level(self, level): |
481 |
if type(level) == str: |
|
482 |
if level.lower() == 'autocommit': |
|
483 |
return psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT |
|
484 |
if level.lower() == 'read_committed': |
|
485 |
return psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED |
|
486 |
if level.lower() == 'serializable': |
|
487 |
return psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE |
|
488 |
raise PgManagerError('Unknown isolation level name: "%s"', level) |
|
489 |
return level |
|
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
490 |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
491 |
def _check_fork(self): |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
492 |
'''Check if process was forked (PID has changed). |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
493 |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
494 |
If it was, clean parent's connections. |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
495 |
New connections are created for children. |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
496 |
Known connection credentials are inherited, but not shared. |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
497 |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
498 |
''' |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
499 |
if self.pid == multiprocessing.current_process().pid: |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
500 |
# PID has not changed |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
501 |
return |
43
a921669e913a
PgManager: rewrite RowDict class.
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
502 |
|
24
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
503 |
# update saved PID |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
504 |
self.pid = multiprocessing.current_process().pid |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
505 |
# reinitialize lock |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
506 |
self.lock = threading.Lock() |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
507 |
# clean parent's connections |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
508 |
for name in self.conn_pool: |
5664afa530e5
PgManager: Add partial support for multiprocessing.
Radek Brich <radek.brich@devl.cz>
parents:
23
diff
changeset
|
509 |
self.conn_pool[name] = [] |
0 | 510 |
|
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
|
511 |
@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
|
512 |
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
|
513 |
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
|
514 |
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
|
515 |
return cls._instance |
0 | 516 |
|
517 |
||
518 |
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
|
519 |
return PgManager.get_instance() |
0 | 520 |