@@ -57,36 +57,35 @@ for hero in marvel_heroes.run(connection):
57
57
```
58
58
59
59
### 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.
62
61
63
62
``` python
64
63
import asyncio
65
64
from rethinkdb import r
66
65
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".
70
66
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())
90
89
```
91
90
92
91
### Gevent mode
0 commit comments