重新思考数据库服务器的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第三方库


热门话题
ArrayList Java中的搜索字符串   另一个web应用程序的java访问会话   另一个应用程序中的活动和服务之间的java通信   java根据Json字符串类型将Json字符串转换为对象   eclipse如何解决java中的错误异常。lang.NoSuchMethodError:'java。字符串javax。摆动JOptionPane。showInputDialog(java.lang.String)'   线程“main”java中的安卓异常。lang.NoClassDefFoundError:org/codehaus/jackson/JsonParseException   java如何在安卓 emulator上显示Mat图像?使用NDK   Java在本地读取测试源文件,但在服务器上读取失败   java dowhile循环用于计算输入数字中的数字。故障排除代码   JAva初学者在编写获取成本的方法时遇到困难   java是shell游戏。我如何让物体移动,特别是在特定的曲线上,但顺序是随机的?   java如何区分两个同名的JButton   java为什么我在Spring Boot中需要一个接口?   java将文件路径插入数据库将删除\   使用InterfaceType初始化java对象   java如何部署一个分为Angular、Spring Boot和MySQL的项目?   java如何使用Symja解决不等式?