Skip to content

Commit ef08ba2

Browse files
committed
Update asyncio example program in README.md
1 parent 406b705 commit ef08ba2

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

README.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,35 @@ for hero in marvel_heroes.run(connection):
5757
```
5858

5959
### Asyncio mode
60-
Asyncio mode is compatible with Python ≥ 3.4, which is when asyncio was
61-
introduced into the standard library.
60+
Asyncio mode is compatible with Python ≥ 3.5.
6261

6362
```python
6463
import asyncio
6564
from rethinkdb import r
6665

67-
# Native coroutines are supported in Python ≥ 3.5. In Python 3.4, you should
68-
# use the @asyncio.couroutine decorator instead of "async def", and "yield from"
69-
# instead of "await".
7066
async def main():
71-
r.set_loop_type('asyncio')
72-
connection = await r.connect(db='test')
73-
74-
await r.table_create('marvel').run(connection)
75-
76-
marvel_heroes = r.table('marvel')
77-
await marvel_heroes.insert({
78-
'id': 1,
79-
'name': 'Iron Man',
80-
'first_appearance': 'Tales of Suspense #39'
81-
}).run(connection)
82-
83-
# "async for" is supported in Python ≥ 3.6. In earlier versions, you should
84-
# call "await cursor.next()" in a loop.
85-
cursor = await marvel_heroes.run(connection)
86-
async for hero in cursor:
87-
print(hero['name'])
88-
89-
asyncio.get_event_loop().run_until_complete(main())
67+
async with await r.connect(db='test') as connection:
68+
await r.table_create('marvel').run(connection)
69+
70+
marvel_heroes = r.table('marvel')
71+
await marvel_heroes.insert({
72+
'id': 1,
73+
'name': 'Iron Man',
74+
'first_appearance': 'Tales of Suspense #39'
75+
}).run(connection)
76+
77+
# "async for" is supported in Python ≥ 3.6. In earlier versions, you should
78+
# call "await cursor.next()" in a loop.
79+
cursor = await marvel_heroes.run(connection)
80+
async for hero in cursor:
81+
print(hero['name'])
82+
# The `with` block performs `await connection.close(noreply_wait=False)`.
83+
84+
r.set_loop_type('asyncio')
85+
86+
# "asyncio.run" was added in Python 3.7. In earlier versions, you
87+
# might try asyncio.get_event_loop().run_until_complete(main()).
88+
asyncio.run(main())
9089
```
9190

9291
### Gevent mode

0 commit comments

Comments
 (0)