Simple async runner that cleans up DB connections on exit. This is meant for simple scripts. Usage:: from tortoise import Tortoise, run_async async def do_stuff(): await Tortoise.init( db_url='sqlite://db.sqlite3', models={'
(coro: Coroutine)
| 553 | |
| 554 | |
| 555 | def run_async(coro: Coroutine) -> None: |
| 556 | """ |
| 557 | Simple async runner that cleans up DB connections on exit. |
| 558 | This is meant for simple scripts. |
| 559 | |
| 560 | Usage:: |
| 561 | |
| 562 | from tortoise import Tortoise, run_async |
| 563 | |
| 564 | async def do_stuff(): |
| 565 | await Tortoise.init( |
| 566 | db_url='sqlite://db.sqlite3', |
| 567 | models={'models': ['app.models']} |
| 568 | ) |
| 569 | |
| 570 | ... |
| 571 | |
| 572 | run_async(do_stuff()) |
| 573 | """ |
| 574 | from tortoise.context import get_current_context |
| 575 | |
| 576 | async def main() -> None: |
| 577 | try: |
| 578 | await coro |
| 579 | finally: |
| 580 | ctx = get_current_context() |
| 581 | if ctx is not None: |
| 582 | await ctx.connections.close_all(discard=True) |
| 583 | |
| 584 | with from_thread.start_blocking_portal() as portal: |
| 585 | portal.call(main) |
| 586 | |
| 587 | |
| 588 | __version__ = "1.1.7" |
no outgoing calls
searching dependent graphs…