"""Illustrates the asyncio engine / connection interface.In this example, we have an async engine created by:func:`_engine.create_async_engine`. We then use it using awaitwithin a coroutine."""importasynciofromsqlalchemyimportColumnfromsqlalchemyimportIntegerfromsqlalchemyimportMetaDatafromsqlalchemyimportStringfromsqlalchemyimportTablefromsqlalchemy.ext.asyncioimportcreate_async_enginemeta=MetaData()t1=Table("t1",meta,Column("id",Integer,primary_key=True),Column("name",String))asyncdefasync_main():# engine is an instance of AsyncEngineengine=create_async_engine("postgresql+asyncpg://scott:tiger@localhost/test",echo=True,)# conn is an instance of AsyncConnectionasyncwithengine.begin()asconn:# to support SQLAlchemy DDL methods as well as legacy functions, the# AsyncConnection.run_sync() awaitable method will pass a "sync"# version of the AsyncConnection object to any synchronous method,# where synchronous IO calls will be transparently translated for# await.awaitconn.run_sync(meta.drop_all)awaitconn.run_sync(meta.create_all)# for normal statement execution, a traditional "await execute()"# pattern is used.awaitconn.execute(t1.insert(),[{"name":"some name 1"},{"name":"some name 2"}])asyncwithengine.connect()asconn:# the default result object is the# sqlalchemy.engine.Result objectresult=awaitconn.execute(t1.select())# the results are buffered so no await call is necessary# for this case.print(result.fetchall())# for a streaming result that buffers only segments of the# result at time, the AsyncConnection.stream() method is used.# this returns a sqlalchemy.ext.asyncio.AsyncResult object.async_result=awaitconn.stream(t1.select())# this object supports async iteration and awaitable# versions of methods like .all(), fetchmany(), etc.asyncforrowinasync_result:print(row)asyncio.run(async_main())