Skip to content

Commit de4bcb8

Browse files
committed
feat: docs updated
1 parent 2ca8a7a commit de4bcb8

File tree

7 files changed

+190
-108
lines changed

7 files changed

+190
-108
lines changed

docs/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ Contents
133133

134134
peewee_async/api
135135
peewee_async/api_older
136-
peewee_async/tornado
137136
peewee_async/examples
138137

139138
Indices and tables

docs/peewee_async/api.rst

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
1-
High-level (new) API
1+
API Documentation
22
====================
33

4-
High-level API provides a single point for all async ORM calls. Meet the :class:`.Manager` class! The idea of ``Manager`` originally comes from `Django`_, but it's redesigned to meet new `asyncio`_ patterns.
5-
6-
First of all, once ``Manager`` is initialized with database and event loop, it's easy and safe to perform async calls. And all async operations and transactions management methods are bundled with a single object. No need to pass around database instance, event loop, etc.
7-
8-
Also there's no need to connect and re-connect before executing async queries with manager! It's all automatic. But you can run ``Manager.connect()`` or ``Manager.close()`` when you need it.
9-
10-
.. _peewee: https://github.com/coleifer/peewee
11-
.. _Django: https://www.djangoproject.com
12-
.. _asyncio: https://docs.python.org/3/library/asyncio.html
13-
14-
OK, let's provide an example::
4+
Let's provide an example::
155

166
import asyncio
177
import peewee
188
import logging
19-
from peewee_async import Manager, PostgresqlDatabase
9+
from peewee_async import PostgresqlDatabase
2010

21-
loop = asyncio.new_event_loop() # Note: custom loop!
2211
database = PostgresqlDatabase('test')
23-
objects = Manager(database, loop=loop)
24-
25-
-- once ``objects`` is created with specified ``loop``, all database connections **automatically** will be set up on **that loop**. Sometimes, it's so easy to forget to pass custom loop instance, but now it's not a problem! Just initialize with an event loop once.
2612

27-
Let's define a simple model::
13+
# Disable sync queries
14+
database.set_allow_sync(False)
2815

16+
# Let's define a simple model:
2917
class PageBlock(peewee_async.AioModel):
3018
key = peewee.CharField(max_length=40, unique=True)
3119
text = peewee.TextField(default='')
3220

3321
class Meta:
3422
database = database
3523

36-
-- as you can see, nothing special in this code, just plain ``peewee_async.AioModel`` definition.
24+
-- as you can see, nothing special in this code, just plain ``peewee_async.AioModel`` definition and disabling sync queries.
3725

3826
Now we need to create a table for model::
3927

40-
PageBlock.create_table(True)
28+
with database.allow_sync():
29+
PageBlock.create_table(True)
4130

4231
-- this code is **sync**, and will do **absolutely the same thing** as would do code with regular ``peewee.PostgresqlDatabase``. This is intentional, I believe there's just no need to run database initialization code asynchronously! *Less code, less errors*.
4332

@@ -56,75 +45,43 @@ Finally, let's do something async::
5645

5746
# Save with new text using manager
5847
title.text = "Peewee is SUPER awesome with async!"
59-
await objects.update(title)
48+
await title.aio_save()
6049
print("New:", title.text)
6150

6251
loop.run_until_complete(my_async_func())
6352
loop.close()
6453

65-
**That's it!**
54+
**That's it!** As you may notice there's no need to connect and re-connect before executing async queries! It's all automatic. But you can run ``AioDatabase.aio_connect()`` or ``AioDatabase.aio_close()`` when you need it.
6655

67-
As you may notice you can use methods from **Manager** or from **AioModel** for operations like selecting, deleting etc.
56+
And you can use methods from from **AioModel** for operations like selecting, deleting etc.
6857
All of them are listed below.
6958

70-
Manager
71-
-------
72-
73-
.. autoclass:: peewee_async.Manager
74-
75-
.. autoattribute:: peewee_async.Manager.database
76-
77-
.. automethod:: peewee_async.Manager.allow_sync
78-
79-
.. automethod:: peewee_async.Manager.get
80-
81-
.. automethod:: peewee_async.Manager.create
8259

83-
.. automethod:: peewee_async.Manager.update
84-
85-
.. automethod:: peewee_async.Manager.delete
86-
87-
.. automethod:: peewee_async.Manager.get_or_create
88-
89-
.. automethod:: peewee_async.Manager.create_or_get
90-
91-
.. automethod:: peewee_async.Manager.execute
92-
93-
.. automethod:: peewee_async.Manager.prefetch
60+
Databases
61+
---------
9462

95-
.. automethod:: peewee_async.Manager.count
63+
.. autoclass:: peewee_async.databases.AioDatabase
9664

97-
.. automethod:: peewee_async.Manager.scalar
65+
.. automethod:: peewee_async.databases.AioDatabase.aio_connect
9866

99-
.. automethod:: peewee_async.Manager.connect
67+
.. autoproperty:: peewee_async.databases.AioDatabase.is_connected
10068

101-
.. automethod:: peewee_async.Manager.close
69+
.. automethod:: peewee_async.databases.AioDatabase.aio_close
10270

103-
.. automethod:: peewee_async.Manager.atomic
71+
.. automethod:: peewee_async.databases.AioDatabase.aio_execute
10472

105-
.. automethod:: peewee_async.Manager.transaction
73+
.. automethod:: peewee_async.databases.AioDatabase.set_allow_sync
10674

107-
.. automethod:: peewee_async.Manager.savepoint
75+
.. automethod:: peewee_async.databases.AioDatabase.allow_sync
10876

109-
110-
Databases
111-
---------
112-
113-
.. autoclass:: peewee_async.PostgresqlDatabase
114-
:members: init
77+
.. automethod:: peewee_async.databases.AioDatabase.aio_atomic
11578

11679
.. autoclass:: peewee_async.PooledPostgresqlDatabase
11780
:members: init
11881

119-
.. autoclass:: peewee_asyncext.PostgresqlExtDatabase
120-
:members: init
121-
12282
.. autoclass:: peewee_asyncext.PooledPostgresqlExtDatabase
12383
:members: init
12484

125-
.. autoclass:: peewee_async.MySQLDatabase
126-
:members: init
127-
12885
.. autoclass:: peewee_async.PooledMySQLDatabase
12986
:members: init
13087

@@ -138,3 +95,26 @@ AioModel
13895
.. automethod:: peewee_async.AioModel.aio_get_or_none
13996

14097
.. automethod:: peewee_async.AioModel.aio_create
98+
99+
.. automethod:: peewee_async.AioModel.aio_get_or_create
100+
101+
.. automethod:: peewee_async.AioModel.aio_delete_instance
102+
103+
.. automethod:: peewee_async.AioModel.aio_save
104+
105+
.. autofunction:: peewee_async.aio_prefetch
106+
107+
AioModelSelect
108+
--------------
109+
110+
.. autoclass:: peewee_async.aio_model.AioModelSelect
111+
112+
.. automethod:: peewee_async.aio_model.AioModelSelect.aio_scalar
113+
114+
.. automethod:: peewee_async.aio_model.AioModelSelect.aio_get
115+
116+
.. automethod:: peewee_async.aio_model.AioModelSelect.aio_count
117+
118+
.. automethod:: peewee_async.aio_model.AioModelSelect.aio_exists
119+
120+
.. automethod:: peewee_async.aio_model.AioModelSelect.aio_prefetch

docs/peewee_async/api_older.rst

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Low-level (older) API
1+
Deprecated API
22
=====================
33

44
**Note:** all query methods are **coroutines**.
@@ -34,14 +34,50 @@ Databases
3434
:members: connect_async, atomic_async, transaction_async, savepoint_async
3535
:noindex:
3636

37-
.. autoclass:: peewee_async.PooledPostgresqlDatabase
38-
:members: connect_async
39-
:noindex:
40-
4137
.. autoclass:: peewee_asyncext.PostgresqlExtDatabase
4238
:members: connect_async, atomic_async, transaction_async, savepoint_async
4339
:noindex:
4440

45-
.. autoclass:: peewee_asyncext.PooledPostgresqlExtDatabase
46-
:members: connect_async
41+
.. autoclass:: peewee_async.MySQLDatabase
42+
:members: connect_async, atomic_async, transaction_async, savepoint_async
4743
:noindex:
44+
45+
46+
Manager
47+
-------
48+
49+
.. autoclass:: peewee_async.Manager
50+
51+
.. autoattribute:: peewee_async.Manager.database
52+
53+
.. automethod:: peewee_async.Manager.allow_sync
54+
55+
.. automethod:: peewee_async.Manager.get
56+
57+
.. automethod:: peewee_async.Manager.create
58+
59+
.. automethod:: peewee_async.Manager.update
60+
61+
.. automethod:: peewee_async.Manager.delete
62+
63+
.. automethod:: peewee_async.Manager.get_or_create
64+
65+
.. automethod:: peewee_async.Manager.create_or_get
66+
67+
.. automethod:: peewee_async.Manager.execute
68+
69+
.. automethod:: peewee_async.Manager.prefetch
70+
71+
.. automethod:: peewee_async.Manager.count
72+
73+
.. automethod:: peewee_async.Manager.scalar
74+
75+
.. automethod:: peewee_async.Manager.connect
76+
77+
.. automethod:: peewee_async.Manager.close
78+
79+
.. automethod:: peewee_async.Manager.atomic
80+
81+
.. automethod:: peewee_async.Manager.transaction
82+
83+
.. automethod:: peewee_async.Manager.savepoint

docs/peewee_async/examples.rst

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
More examples
1+
Examples
22
=============
33

44

@@ -20,10 +20,10 @@ Using both sync and async calls
2020
class Meta:
2121
database = database
2222
23-
# Create table synchronously!
24-
TestModel.create_table(True)
25-
# This is optional: close sync connection
26-
database.close()
23+
async with database.allow_sync():
24+
# Create table synchronously!
25+
TestModel.create_table(True)
26+
# sync connection is closed automatically on exit
2727
2828
async def my_handler():
2929
obj1 = TestModel.create(text="Yo, I can do it sync!")
@@ -55,7 +55,7 @@ Using transactions
5555
obj_id = obj.id
5656
5757
try:
58-
async with database.atomic_async():
58+
async with database.aio_atomic():
5959
await TestModel.update(text='BAR').where(TestModel.id == obj_id).aio_execute()
6060
raise Exception('Fake error')
6161
except:
@@ -64,3 +64,29 @@ Using transactions
6464
print(res.text) # Should print 'FOO', not 'BAR'
6565
6666
loop.run_until_complete(test())
67+
68+
Using async peewee with Tornado
69+
-------------------------------
70+
71+
`Tornado`_ is a mature and powerful asynchronous web framework. It provides its own event loop, but there's an option to run Tornado on asyncio event loop. And that's exactly what we need!
72+
73+
.. _Tornado: http://www.tornadoweb.org
74+
75+
The complete working example is provided below. And here are some general notes:
76+
77+
1. **Be aware of current asyncio event loop!**
78+
79+
In the provided example we use the default event loop everywhere, and that's OK. But if you see your application got silently stuck, that's most probably that some task is started on the different loop and will never complete as long as that loop is not running.
80+
81+
2. Tornado request handlers **does not** start asyncio tasks by default.
82+
83+
The ``CreateHandler`` demostrates that, ``current_task()`` returns ``None`` until task is run explicitly.
84+
85+
3. Transactions **must** run within task context.
86+
87+
All transaction operations have to be done within task. So if you need to run a transaction from Tornado handler, you have to wrap your call into task with ``create_task()`` or ``ensure_future()``.
88+
89+
**Also note:** if you spawn an extra task during a transaction, it will run outside of that transaction.
90+
91+
.. literalinclude:: ../../examples/tornado_sample.py
92+
:start-after: # Start example

docs/peewee_async/tornado.rst

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)