rethinkpool
is a Python library for pooling RethinkDB connections.
This is usefull for multi-thread application since a connection object Python driver provides is not thread-safe
$ pip install rethinkpool
Connection Pool can be created by instantiating RethinkPool
.
from rethinkpool import RethinkPool
pool = RethinkPool()
The arguments for RethinkPool
are as follows.
- max_conns: maximum number of connections
- initial_conns: number of initial connections to establish
- get_timeout: timeout for obtaining a connection from the queue
- host, port, ...: same as
r.connect
The pooled ConnectionResource
object can be obtained by pool.get_resource()
Used connection resource can be returned to the pool as follows.
res = pool.get_resource()
cur = r.table("data").run(res.conn)
for obj in cur:
print(obj)
res.release()
It also supports with
statement for automatically restore used connection back to the pool.
with pool.get_resource() as res:
cur = r.table("data").run(res.conn)
for obj in cur:
print(obj)