Package: rethinkdb
RethinkDB Python driverOverviewWhat is RethinkDB?RethinkDB is the first open-source scalable database built for realtime applications. It exposes a new database access model -- instead of polling for changes, the developer can tell the database to continuously push updated query results to applications in realtime. RethinkDB allows developers to build scalable realtime apps in a fraction of the time with less effort.Installation$pipinstallrethinkdbNote: this package is the extracted driver of RethinkDB's original python driver.QuickstartThe main difference with the previous driver (except the name of the package) is we arenotimporting RethinkDB asr. If you would like to useRethinkDB's python driver as a drop in replacement, you should do the following:fromrethinkdbimportrconnection=r.connect(db='test')Blocking and Non-blocking I/OThis driver supports blocking I/O (i.e. standard Python sockets) as well as
non-blocking I/O through multiple async frameworks:AsyncioGeventTornadoTrioTwistedThe following examples demonstrate how to use the driver in each mode.Default mode (blocking I/O)The driver's default mode of operation is to use blocking I/O, i.e. standard Python
sockets. This example shows how to create a table, populate with data, and get every
document.fromrethinkdbimportrconnection=r.connect(db='test')r.table_create('marvel').run(connection)marvel_heroes=r.table('marvel')marvel_heroes.insert({'id':1,'name':'Iron Man','first_appearance':'Tales of Suspense #39'}).run(connection)forheroinmarvel_heroes.run(connection):print(hero['name'])Asyncio modeAsyncio mode is compatible with Python ≥ 3.5.importasynciofromrethinkdbimportrasyncdefmain():asyncwithawaitr.connect(db='test')asconnection:awaitr.table_create('marvel').run(connection)marvel_heroes=r.table('marvel')awaitmarvel_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=awaitmarvel_heroes.run(connection)asyncforheroincursor: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 modeimportgeventfromrethinkdbimportrdefmain():r.set_loop_type('gevent')connection=r.connect(db='test')r.table_create('marvel').run(connection)marvel_heroes=r.table('marvel')marvel_heroes.insert({'id':1,'name':'Iron Man','first_appearance':'Tales of Suspense #39'}).run(connection)forheroinmarvel_heroes.run(connection):print(hero['name'])gevent.joinall([gevent.spawn(main)])Tornado modeTornado mode is compatible with Tornado < 5.0.0. Tornado 5 is not supported.fromrethinkdbimportrfromtornadoimportgenfromtornado.ioloopimportIOLoop@gen.coroutinedefmain():r.set_loop_type('tornado')connection=yieldr.connect(db='test')yieldr.table_create('marvel').run(connection)marvel_heroes=r.table('marvel')yieldmarvel_heroes.insert({'id':1,'name':'Iron Man','first_appearance':'Tales of Suspense #39'}).run(connection)cursor=yieldmarvel_heroes.run(connection)while(yieldcursor.fetch_next()):hero=yieldcursor.next()print(hero['name'])IOLoop.current().run_sync(main)Trio modefromrethinkdbimportrimporttrioasyncdefmain():r.set_loop_type('trio')asyncwithtrio.open_nursery()asnursery:asyncwithr.open(db='test',nursery=nursery)asconn:awaitr.table_create('marvel').run(conn)marvel_heroes=r.table('marvel')awaitmarvel_heroes.insert({'id':1,'name':'Iron Man','first_appearance':'Tales of Suspense #39'}).run(conn)# "async for" is supported in Python ≥ 3.6. In earlier versions, you should# call "await cursor.next()" in a loop.cursor=awaitmarvel_heroes.run(conn)asyncwithcursor:asyncforheroincursor:print(hero['name'])trio.run(main)The Trio mode also supports a database connection pool. You can modify the example above
as follows:db_pool=r.ConnectionPool(db='test',nursery=nursery)asyncwithdb_pool.connection()asconn:...awaitdb_pool.close()Twisted modefromrethinkdbimportrfromtwisted.internetimportreactor,defer@defer.inlineCallbacksdefmain():r.set_loop_type('twisted')connection=yieldr.connect(db='test')yieldr.table_create('marvel').run(connection)marvel_heroes=r.table('marvel')yieldmarvel_heroes.insert({'id':1,'name':'Iron Man','first_appearance':'Tales of Suspense #39'}).run(connection)cursor=yieldmarvel_heroes.run(connection)while(yieldcursor.fetch_next()):hero=yieldcursor.next()print(hero['name'])main().addCallback(lambdad:print("stopping")orreactor.stop())reactor.run()MiscTo help the migration from rethinkdb<2.4 we introduced a shortcut which can easily replace the oldimport rethinkdb as rimport withfrom rethinkdb import r.Run testsIn theMakefileyou can find three different test commands:test-unit,test-integrationandtest-remote. As RethinkDB has dropped the support of Windows, we would like to ensure that those of us who are using Windows for development can still contribute. Because of this, we support running integration tests against Digital Ocean Droplets as well.Before you run any test, make sure that you install the requirements.$pipinstall-rrequirements.txt
$makeprepareRunning unit tests$maketest-unitRunning integration testsTo run integration tests locally, make sure you intstalled RethinkDB$maketest-integrationRunning remote integration testsTo run the remote tests, you need to have a Digital Ocean account and an API key.Remote test will create a new temporary SSH key and a Droplet for you until the tests are finished.Available environment variablesVariable nameDefault valueDO_TOKENN/ADO_SIZE512MBDO_REGIONsfo2$pipinstallparamikopython-digitalocean
$exportDO_TOKEN=<YOUR_TOKEN>
$maketest-remoteContributingHurray! You reached this section which means, that you would like to contribute. Please read our contributing guide lines and feel free to open a pull request.