You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- as you can see, nothing special in this code, just plain ``peewee_async.AioModel`` definition and disabling sync queries.
25
-
26
-
Now we need to create a table for model::
27
-
28
-
with database.allow_sync():
29
-
PageBlock.create_table(True)
30
-
31
-
-- 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*.
32
-
33
-
Finally, let's do something async::
34
-
35
-
async def my_async_func():
36
-
# Add new page block
37
-
await PageBlock.aio_create(
38
-
key='title',
39
-
text="Peewee is AWESOME with async!"
40
-
)
41
-
42
-
# Get one by key
43
-
title = await PageBlock.aio_get(PageBlock, key='title')
44
-
print("Was:", title.text)
45
-
46
-
# Save with new text using manager
47
-
title.text = "Peewee is SUPER awesome with async!"
48
-
await title.aio_save()
49
-
print("New:", title.text)
50
-
51
-
loop.run_until_complete(my_async_func())
52
-
loop.close()
53
-
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.
55
-
56
-
And you can use methods from from **AioModel** for operations like selecting, deleting etc.
Copy file name to clipboardexpand all lines: docs/peewee_async/examples.rst
+3-3
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ Examples
3
3
4
4
5
5
Using both sync and async calls
6
-
-------------------------------
6
+
+++++++++++++++++++++++++++++++
7
7
8
8
.. code-block:: python
9
9
@@ -40,7 +40,7 @@ Using both sync and async calls
40
40
41
41
42
42
Using transactions
43
-
------------------
43
+
++++++++++++++++++
44
44
45
45
.. code-block:: python
46
46
@@ -66,7 +66,7 @@ Using transactions
66
66
loop.run_until_complete(test())
67
67
68
68
Using async peewee with Tornado
69
-
-------------------------------
69
+
+++++++++++++++++++++++++++++++
70
70
71
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!
-- as you can see, nothing special in this code, just plain ``peewee_async.AioModel`` definition and disabling sync queries.
25
+
26
+
Now we need to create a table for model::
27
+
28
+
with database.allow_sync():
29
+
PageBlock.create_table(True)
30
+
31
+
-- 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*.
32
+
33
+
Finally, let's do something async::
34
+
35
+
async def my_async_func():
36
+
# Add new page block
37
+
await PageBlock.aio_create(
38
+
key='title',
39
+
text="Peewee is AWESOME with async!"
40
+
)
41
+
42
+
# Get one by key
43
+
title = await PageBlock.aio_get(key='title')
44
+
print("Was:", title.text)
45
+
46
+
# Save with new text using manager
47
+
title.text = "Peewee is SUPER awesome with async!"
48
+
await title.aio_save()
49
+
print("New:", title.text)
50
+
51
+
loop.run_until_complete(my_async_func())
52
+
loop.close()
53
+
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.
55
+
56
+
And you can use methods from from **AioModel** for operations like selecting, deleting etc.
0 commit comments