Skip to content

Commit 2b17b14

Browse files
committed
docs: Improve documentation
1 parent 42812ef commit 2b17b14

File tree

5 files changed

+147
-49
lines changed

5 files changed

+147
-49
lines changed

asyncpg/connection.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class Connection:
2424
"""A representation of a database session.
2525
26-
Connections are created by calling :func:`~asyncpg.connect`.
26+
Connections are created by calling :func:`~asyncpg.connection.connect`.
2727
"""
2828

2929
__slots__ = ('_protocol', '_transport', '_loop', '_types_stmt',
@@ -361,7 +361,54 @@ async def connect(dsn=None, *,
361361
statement_cache_size=100,
362362
command_timeout=None,
363363
**opts):
364+
"""A coroutine to establish a connection to a PostgreSQL server.
364365
366+
Returns a new :class:`~asyncpg.connection.Connection` object.
367+
368+
:param dsn: Connection arguments specified using as a single string in the
369+
following format:
370+
``postgres://user:pass@host:port/database?option=value``
371+
372+
:param host: database host address or a path to the directory containing
373+
database server UNIX socket (defaults to the default UNIX
374+
socket, or the value of the ``PGHOST`` environment variable,
375+
if set).
376+
377+
:param port: connection port number (defaults to ``5432``, or the value of
378+
the ``PGPORT`` environment variable, if set)
379+
380+
:param user: the name of the database role used for authentication
381+
(defaults to the name of the effective user of the process
382+
making the connection, or the value of ``PGUSER`` environment
383+
variable, if set)
384+
385+
:param password: password used for authentication
386+
387+
:param loop: An asyncio event loop instance. If ``None``, the default
388+
event loop will be used.
389+
390+
:param float timeout: connection timeout in seconds.
391+
392+
:param float command_timeout: the default timeout for operations on
393+
this connection (the default is no timeout).
394+
395+
:param int statement_cache_size: the size of prepared statement LRU cache.
396+
397+
:return: A :class:`~asyncpg.connection.Connection` instance.
398+
399+
Example:
400+
401+
.. code-block:: pycon
402+
403+
>>> import asyncpg
404+
>>> import asyncio
405+
>>> async def run():
406+
... con = await asyncpg.connect(user='postgres')
407+
... types = await con.fetch('SELECT * FROM pg_type')
408+
... print(types)
409+
>>> asyncio.get_event_loop().run_until_complete(run())
410+
[<Record typname='bool' typnamespace=11 ...
411+
"""
365412
if loop is None:
366413
loop = asyncio.get_event_loop()
367414

asyncpg/transaction.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ class TransactionState(enum.Enum):
2424
class Transaction:
2525
"""Represents a transaction or savepoint block.
2626
27-
Transactions are created by calling
28-
:meth:`Connection.transaction`.
27+
Transactions are created by calling the
28+
:meth:`Connection.transaction() <connection.Connection.transaction>`
29+
function.
2930
"""
3031

3132
__slots__ = ('_connection', '_isolation', '_readonly', '_deferrable',

bench.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import asyncio
2+
import aiopg
3+
import asyncpg
4+
import time
5+
import uvloop
6+
7+
8+
async def bench_asyncpg(iters, size, query):
9+
con = await asyncpg.connect(user='postgres', host='127.0.0.1',
10+
command_timeout=60)
11+
12+
started = time.monotonic()
13+
for _ in range(iters):
14+
await con.fetch(query)
15+
print('-> asyncpg: {:.3} seconds'.format(time.monotonic() - started))
16+
17+
18+
async def bench_asyncpg_execute(iters, size, query):
19+
con = await asyncpg.connect(user='postgres', host='127.0.0.1')
20+
21+
started = time.monotonic()
22+
for _ in range(iters):
23+
await con.execute(query)
24+
print('-> asyncpg execute: {:.3} seconds'.format(
25+
time.monotonic() - started))
26+
27+
28+
async def bench_aiopg(iters, size, query):
29+
con = await aiopg.connect(user='postgres', host='127.0.0.1')
30+
31+
started = time.monotonic()
32+
for _ in range(iters):
33+
cur = await con.cursor(timeout=60)
34+
await cur.execute(query)
35+
await cur.fetchall()
36+
37+
print('-> aiopg: {:.3} seconds'.format(time.monotonic() - started))
38+
39+
con.close()
40+
41+
42+
async def print_debug(loop):
43+
while True:
44+
print(chr(27) + "[2J") # clear screen
45+
loop.print_debug_info()
46+
await asyncio.sleep(0.5, loop=loop)
47+
48+
49+
if __name__ == '__main__':
50+
iters = 10000
51+
size = 1000
52+
53+
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
54+
loop = asyncio.get_event_loop()
55+
56+
# loop.create_task(print_debug(loop))
57+
58+
query = 'select generate_series(0,{})'.format(size)
59+
query = '''select typname, typnamespace, typowner, typlen, typbyval,
60+
typcategory, typispreferred, typisdefined,
61+
typdelim, typrelid, typelem, typarray from pg_type'''
62+
63+
print(loop)
64+
print('iters: {}; \nquery: {}'.format(iters, query))
65+
66+
loop.run_until_complete(bench_aiopg(iters, size, query))
67+
loop.run_until_complete(bench_asyncpg(iters, size, query))
68+
# loop.run_until_complete(bench_asyncpg_execute(iters, size, query))

docs/api/index.rst

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,10 @@ API Reference
1515
Connection
1616
==========
1717

18-
.. coroutinefunction:: connect(dsn=None, *, host=None, port=None, \
19-
user=None, password=None, \
20-
database=None, loop=None, timeout=60, \
21-
statement_cache_size=100, \
22-
command_timeout=None, \
23-
**opts)
18+
.. autofunction:: asyncpg.connection.connect
2419

25-
Establish a connection to a PostgreSQL server and return a new
26-
:class:`~asyncpg.connection.Connection` object.
2720

28-
:param dsn: Connection arguments specified using as a single string in the
29-
following format:
30-
``postgres://user:pass@host:port/database?option=value``
31-
32-
:param host: database host address or a path to the directory containing
33-
database server UNIX socket (defaults to the default UNIX
34-
socket, or the value of the ``PGHOST`` environment variable,
35-
if set).
36-
37-
:param port: connection port number (defaults to ``5432``, or the value of
38-
the ``PGPORT`` environment variable, if set)
39-
40-
:param user: the name of the database role used for authentication
41-
(defaults to the name of the effective user of the process
42-
making the connection, or the value of ``PGUSER`` environment
43-
variable, if set)
44-
45-
:param password: password used for authentication
46-
47-
:param loop: An asyncio event loop instance. If ``None``, the default
48-
event loop will be used.
49-
50-
:param float timeout: connection timeout (in seconds, defaults to 60
51-
seconds).
52-
53-
:param float statement_timeout: the default timeout for operations on
54-
this connection (the default is no timeout).
55-
56-
:param int statement_cache_size: the size of prepared statement LRU cache
57-
(defaults to 100).
58-
59-
:returns: :class:`~asyncpg.connection.Connection` instance.
60-
61-
62-
.. autoclass:: asyncpg.connection.Connection()
21+
.. autoclass:: asyncpg.connection.Connection
6322
:members:
6423

6524

@@ -149,6 +108,10 @@ Alternatively, transactions can be used without an ``async with`` block:
149108
await tr.commit()
150109
151110
111+
See also the
112+
:meth:`Connection.transaction() <asyncpg.connection.Connection.transaction>`
113+
function.
114+
152115
.. _savepoint: https://www.postgresql.org/docs/current/static/sql-savepoint.html
153116

154117

@@ -233,15 +196,15 @@ It's also possible to create cursors from prepared statements:
233196
.. note::
234197

235198
Cursors created by a call to
236-
:meth:`Conection.cursor() <asyncpg.connection.Connection.cursor>` or
199+
:meth:`Connection.cursor() <asyncpg.connection.Connection.cursor>` or
237200
:meth:`PreparedStatement.cursor() <asyncpg.prepared_stmt.PreparedStatement.cursor>`
238201
are *non-scrollable*: they can only be read forwards. To create a scrollable
239202
cursor, use the ``DECLARE ... SCROLL CURSOR`` SQL statement directly.
240203

241204
.. warning::
242205

243206
Cursors created by a call to
244-
:meth:`Conection.cursor() <asyncpg.connection.Connection.cursor>` or
207+
:meth:`Connection.cursor() <asyncpg.connection.Connection.cursor>` or
245208
:meth:`PreparedStatement.cursor() <asyncpg.prepared_stmt.PreparedStatement.cursor>`
246209
cannot be used outside of a transaction. Any such attempt will result in
247210
:exc:`~asyncpg.exceptions.InterfaceError`.
@@ -268,6 +231,18 @@ It's also possible to create cursors from prepared statements:
268231
:members:
269232

270233

234+
.. _asyncpg-api-pool:
235+
236+
Connection Pool
237+
===============
238+
239+
.. autofunction:: asyncpg.pool.create_pool
240+
241+
242+
.. autoclass:: asyncpg.pool.Pool()
243+
:members:
244+
245+
271246
.. _asyncpg-api-record:
272247

273248
Record Objects
@@ -284,9 +259,14 @@ of values either by a numeric index or by a field name:
284259
>>> import asyncio
285260
>>> loop = asyncio.get_event_loop()
286261
>>> conn = loop.run_until_complete(asyncpg.connect())
287-
>>> loop.run_until_complete(conn.fetchrow('''
262+
>>> r = loop.run_until_complete(conn.fetchrow('''
288263
... SELECT oid, rolname, rolsuper FROM pg_roles WHERE rolname = user'''))
264+
>>> r
289265
<Record oid=16388 rolname='elvis' rolsuper=True>
266+
>>> r['oid']
267+
16388
268+
>>> r[0]
269+
16388
290270
291271
292272
.. class:: Record()

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
'sphinxcontrib.asyncio',
2525
]
2626

27+
add_module_names = False
28+
2729
templates_path = ['_templates']
2830
source_suffix = '.rst'
2931
master_doc = 'index'

0 commit comments

Comments
 (0)