Skip to content

Commit 8f46adc

Browse files
mehaaseAdam Grandquist
authored andcommitted
Trio (#73)
* Add example code for each of the 4 async frameworks. * Create a Trio context manager for RethinkDB connections * Add a connection pool to the Trio driver. * Fix most codacy issues * Fix more codacy issues * Fix Trio example in README. Use `async with` to ensure cursor is always closed.
1 parent 7f4f6ea commit 8f46adc

File tree

3 files changed

+547
-0
lines changed

3 files changed

+547
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ non-blocking I/O through multiple async frameworks:
2929
* [Asyncio](https://docs.python.org/3/library/asyncio.html)
3030
* [Gevent](http://www.gevent.org/)
3131
* [Tornado](https://www.tornadoweb.org/en/stable/)
32+
* [Trio](https://trio.readthedocs.io/en/latest/)
3233
* [Twisted](https://twistedmatrix.com/trac/)
3334

3435
The following examples demonstrate how to use the driver in each mode.
@@ -148,6 +149,45 @@ def main():
148149
IOLoop.current().run_sync(main)
149150
```
150151

152+
### Trio mode
153+
154+
```python
155+
from rethinkdb import RethinkDB
156+
import trio
157+
158+
async def main():
159+
r = RethinkDB()
160+
r.set_loop_type('trio')
161+
async with trio.open_nursery() as nursery:
162+
async with r.open(db='test', nursery=nursery) as conn:
163+
await r.table_create('marvel').run(conn)
164+
marvel_heroes = r.table('marvel')
165+
await marvel_heroes.insert({
166+
'id': 1,
167+
'name': 'Iron Man',
168+
'first_appearance': 'Tales of Suspense #39'
169+
}).run(conn)
170+
171+
# "async for" is supported in Python ≥ 3.6. In earlier versions, you should
172+
# call "await cursor.next()" in a loop.
173+
cursor = await marvel_heroes.run(conn)
174+
async with cursor:
175+
async for hero in cursor:
176+
print(hero['name'])
177+
178+
trio.run(main)
179+
```
180+
181+
The Trio mode also supports a database connection pool. You can modify the example above
182+
as follows:
183+
184+
```python
185+
db_pool = r.ConnectionPool(db='test', nursery=nursery)
186+
async with db_pool.connection() as conn:
187+
...
188+
await db_pool.close()
189+
```
190+
151191
### Twisted mode
152192

153193
```python

rethinkdb/trio_net/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)