MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / async_main

Function async_main

examples/asyncio/greenlet_orm.py:66–93  ·  view source on GitHub ↗

Main program function.

()

Source from the content-addressed store, hash-verified

64
65
66async def async_main():
67 """Main program function."""
68
69 engine = create_async_engine(
70 "postgresql+asyncpg://scott:tiger@localhost/test",
71 echo=True,
72 )
73 async with engine.begin() as conn:
74 await conn.run_sync(Base.metadata.drop_all)
75 await conn.run_sync(Base.metadata.create_all)
76
77 async with AsyncSession(engine) as session:
78 async with session.begin():
79 session.add_all(
80 [
81 A(bs=[B(), B()], data="a1"),
82 A(bs=[B()], data="a2"),
83 A(bs=[B(), B()], data="a3"),
84 ]
85 )
86
87 # we have the option to run a function written in sync style
88 # within the AsyncSession.run_sync() method. The function will
89 # be passed a synchronous-style Session object and the function
90 # can use traditional ORM patterns.
91 await session.run_sync(run_queries)
92
93 await session.commit()
94
95
96asyncio.run(async_main())

Callers 1

greenlet_orm.pyFile · 0.70

Calls 8

create_async_engineFunction · 0.90
AsyncSessionClass · 0.90
AClass · 0.70
BClass · 0.70
beginMethod · 0.45
run_syncMethod · 0.45
add_allMethod · 0.45
commitMethod · 0.45

Tested by

no test coverage detected