重新思考数据库服务器的python驱动程序库。

rethinkdb的Python项目详细描述


重新思考db python驱动程序

PyPI versionBuild StatusCodacy BadgeCodacy Badge

概述

什么是重新思考数据库?

RethinkDB是第一个为实时应用程序构建的开源可伸缩数据库。它公开了一个新的数据库访问模型——开发人员可以告诉数据库不断地将更新的查询结果实时推送到应用程序,而不是轮询更改。RethinkDB允许开发人员用较少的精力在很短的时间内构建可伸缩的实时应用程序。

安装

$ pip install rethinkdb

注意:这个包是RethinkDB原始Python驱动程序的提取驱动程序。

快速启动

与前一个驱动程序(除了包的名称)的主要区别是,not将rethinkdb导入为r。如果要使用RethinkDB的python驱动程序作为替换,应执行以下操作:

fromrethinkdbimportRethinkDBr=RethinkDB()connection=r.connect(db='test')

阻塞和非阻塞I/O

这个驱动程序支持阻塞i/o(即标准python套接字)以及 通过多个异步框架实现非阻塞I/O:

以下示例演示如何在每个模式下使用驱动程序。

默认模式(阻塞I/O)

驱动程序的默认操作模式是使用阻塞i/o,即标准python 插座。这个例子展示了如何创建一个表,填充数据,并获取 文件。

fromrethinkdbimportRethinkDBr=RethinkDB()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'])

异步模式

asyncio模式与python≥3.4兼容,即asyncio 引入标准库。

importasynciofromrethinkdbimportRethinkDB# 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".asyncdefmain():r=RethinkDB()r.set_loop_type('asyncio')connection=awaitr.connect(db='test')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'])asyncio.get_event_loop().run_until_complete(main())

gevent模式

importgeventfromrethinkdbimportRethinkDBdefmain():r=RethinkDB()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模式与Tornado<;5.0.0兼容。不支持Tornado 5。

fromrethinkdbimportRethinkDBfromtornadoimportgenfromtornado.ioloopimportIOLoop@gen.coroutinedefmain():r=RethinkDB()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)

三重模式

fromrethinkdbimportRethinkDBimporttrioasyncdefmain():r=RethinkDB()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)

trio模式还支持数据库连接池。您可以修改上面的示例 如下所示:

db_pool=r.ConnectionPool(db='test',nursery=nursery)asyncwithdb_pool.connection()asconn:...awaitdb_pool.close()

扭曲模式

fromrethinkdbimportRethinkDBfromtwisted.internetimportreactor,defer@defer.inlineCallbacksdefmain():r=RethinkDB()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()

其他

尽管我们建议使用示例中使用的导入,但为了帮助从RethinkDB<;2.4迁移,我们引入了一个快捷方式,可以轻松地用from rethinkdb import r替换旧的import rethinkdb as r导入。

运行测试

Makefile中,您可以找到三个不同的测试命令:test-unittest-integrationtest-remote。由于RethinkDB已经放弃了对Windows的支持,我们希望确保那些使用Windows进行开发的人仍然能够做出贡献。因此,我们也支持对数字海洋液滴进行集成测试。

在运行任何测试之前,请确保安装了需求。

$ pip install -r requirements.txt
$ make prepare

运行单元测试

$ make test-unit

运行集成测试

若要在本地运行集成测试,请确保已安装RethinkDB

$ make test-integration

运行远程集成测试

要运行远程测试,您需要有一个数字海洋帐户和一个api密钥。

远程测试将为您创建一个新的临时ssh密钥和一个小滴,直到测试完成。

可用环境变量

Variable nameDefault value
DO_TOKENN/A
DO_SIZE512MB
DO_REGIONsfo2
$ pip install paramiko python-digitalocean
$ exportDO_TOKEN=<YOUR_TOKEN>
$ make test-remote

新功能

github的问题跟踪器是only用于报告错误。不接受新功能!使用spectrum作为支持功能。

贡献

万岁!你到了这一部分,意思是,你想贡献。请阅读我们提供的指南,并随时打开一个拉动请求。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用户界面如何在JavaGUI中为按钮创建各种颜色?   使用MSCAPI的RSA密钥包装的java使用   java Spring数据Redis JedisConnectionException:意外的流结束   java饼图未显示预期输出   java hystrixCommand注释commandKey的用途是什么   windows java可以从cmd运行jar,但不能通过双击来运行   java在单击按钮时遍历XML节点   java试图使用日期(int,int,int)构造函数   带有Buildship 2子项目的java不作为项目依赖项链接   java jsonrpc4j:如何从服务器获取列表<SomeObject>?   用于Java应用程序的内存设置MaxDirectMemory和MaxHeapMemory   java如何从以下类型格式化日期   javayoutube。搜索列表搜索不返回任何内容   java My参数在方法中不起作用,因为泛型存在问题   java如何将Map<Key1Type,Val1Type>转换为Map<Key1Type,Val2Type>   JavaJUnit测试:测试用例的改进,测试数组列表的长度?   java如何在Android中解析带有属性值的xml数据?   使用Hibernate对枚举类型进行Java8bean验证