Skip to content

Update README asyncio example #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,35 @@ for hero in marvel_heroes.run(connection):
```

### Asyncio mode
Asyncio mode is compatible with Python ≥ 3.4, which is when asyncio was
introduced into the standard library.
Asyncio mode is compatible with Python ≥ 3.5.

```python
import asyncio
from rethinkdb import r

# Native coroutines are supported in Python ≥ 3.5. In Python 3.4, you should
# use the @asyncio.couroutine decorator instead of "async def", and "yield from"
# instead of "await".
async def main():
r.set_loop_type('asyncio')
connection = await r.connect(db='test')

await r.table_create('marvel').run(connection)

marvel_heroes = r.table('marvel')
await marvel_heroes.insert({
'id': 1,
'name': 'Iron Man',
'first_appearance': 'Tales of Suspense #39'
}).run(connection)

# "async for" is supported in Python ≥ 3.6. In earlier versions, you should
# call "await cursor.next()" in a loop.
cursor = await marvel_heroes.run(connection)
async for hero in cursor:
print(hero['name'])

asyncio.get_event_loop().run_until_complete(main())
async with await r.connect(db='test') as connection:
await r.table_create('marvel').run(connection)

marvel_heroes = r.table('marvel')
await marvel_heroes.insert({
'id': 1,
'name': 'Iron Man',
'first_appearance': 'Tales of Suspense #39'
}).run(connection)

# "async for" is supported in Python ≥ 3.6. In earlier versions, you should
# call "await cursor.next()" in a loop.
cursor = await marvel_heroes.run(connection)
async for hero in cursor:
print(hero['name'])
# The `with` block performs `await connection.close(noreply_wait=False)`.

r.set_loop_type('asyncio')

# "asyncio.run" was added in Python 3.7. In earlier versions, you
# might try asyncio.get_event_loop().run_until_complete(main()).
asyncio.run(main())
```

### Gevent mode
Expand Down