aiopg, aiopg是一个从asyncio访问PostgreSQL数据库的库

分享于 

3分钟阅读

GitHub

  繁體
aiopg is a library for accessing a PostgreSQL database from the asyncio; this fork ports it to trollius
  • 源代码名称:aiopg
  • 源代码网址:http://www.github.com/aio-libs/aiopg
  • aiopg源代码文档
  • aiopg源代码下载
  • Git URL:
    git://www.github.com/aio-libs/aiopg.git
    Git Clone代码到本地:
    git clone http://www.github.com/aio-libs/aiopg
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/aio-libs/aiopg
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    aiopg

    aiopg是一个用于从asyncio(PEP-3156 / tulip)框架访问PostgreSQL数据库的库。它包装了Psycopg数据库驱动程序的异步特性。

    例子

    import asyncioimport aiopg
    dsn ='dbname=aiopg user=aiopg password=passwd host=127.0.0.1'asyncdefgo():
     pool =await aiopg.create_pool(dsn)
     asyncwith pool.acquire() as conn:
     asyncwith conn.cursor() as cur:
     await cur.execute("SELECT 1")
     ret = []
     asyncfor row in cur:
     ret.append(row)
     assert ret == [(1,)]
    loop = asyncio.get_event_loop()
    loop.run_until_complete(go())

    Sqlalchemy的示例可选集成

    import asynciofrom aiopg.sa import create_engineimport sqlalchemy as sa
    metadata = sa.MetaData()
    tbl = sa.Table('tbl', metadata,
     sa.Column('id', sa.Integer, primary_key=True),
     sa.Column('val', sa.String(255)))asyncdefcreate_table(engine):
     asyncwith engine.acquire() as conn:
     await conn.execute('DROP TABLE IF EXISTS tbl')
     await conn.execute('''CREATE TABLE tbl ( id serial PRIMARY KEY, val varchar(255))''')asyncdefgo():
     asyncwith create_engine(user='aiopg',
     database='aiopg',
     host='127.0.0.1',
     password='passwd') as engine:
     asyncwith engine.acquire() as conn:
     await conn.execute(tbl.insert().values(val='abc'))
     asyncfor row in conn.execute(tbl.select()):
     print(row.id, row.val)
    loop = asyncio.get_event_loop()
    loop.run_until_complete(go())

    有关基于yield from的代码,请参见./examples文件夹,文件名中包含old_style part的文件。

    请使用:

    $ make test

    数据  acc  pos  POST  async  postgres  
    相关文章