Я могу запустить код синхронизации следующим образом:
Код: Выделить всё
# contents of 01_hello.py
print( "hello!" )
Код: Выделить всё
$ ipython
Python 3.12.0 (main, Nov 12 2023, 10:40:37) [GCC 11.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.31.0 -- An enhanced Interactive Python. Type '?' for help.
hello
In [1]:
Код: Выделить всё
# contents of 01_hello.py
print( "hello!" )
async def foo():
print( "foo" )
Код: Выделить всё
$ ipython
Python 3.12.0 (main, Nov 12 2023, 10:40:37) [GCC 11.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.31.0 -- An enhanced Interactive Python. Type '?' for help.
hello
In [1]: await foo()
foo
In [2]:
Код: Выделить всё
# contents of 01_hello.py
print( "hello!" )
async def foo():
print( "foo" )
await foo()
Код: Выделить всё
$ ipython
Python 3.12.0 (main, Nov 12 2023, 10:40:37) [GCC 11.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.31.0 -- An enhanced Interactive Python. Type '?' for help.
[TerminalIPythonApp] WARNING | Unknown error in handling startup files:
File ~/proj/.ipython/profile_default/startup/01_imports.py:5
await foo()
^
SyntaxError: 'await' outside function
Код: Выделить всё
asyncio.run()Это не имело бы смысла, поскольку этот цикл событий должен был бы закрыться к концу файла, что делает невозможным выполнение какой-либо работы по инициализации, включающей контекстные переменные (где Tortoise-ORM хранит свои соединения), что противоречит цели.
Или, говоря по-другому: как я могу получить доступ к циклу событий, который запускает IPython для пользы интерактивного оболочка?
Подробнее здесь: https://stackoverflow.com/questions/793 ... rtup-files
Мобильная версия