Код: Выделить всё
async def read_sensor_async():
print("Reading sensor...")
# Actual code
return 9 # Simulate sensor reading
def read_sensor():
return runa(read_sensor_async())
def runa(func):
# Prepare steps...
return asyncio.run(func)
< /code>
Кажется, что до этого момента все в порядке. Однако, когда мы запускаем read_sensor ()
Код: Выделить всё
def runa(func):
# Prepare steps...
with contextlib.suppress(RuntimeError):
return asyncio.run(func)
# Gets here means we are inside an event loop
return await func
обновление
Лучший вопрос: как мы можем переписать функцию read_sensor () , чтобы работать синхронно и асинхронно?>
Подробнее здесь: https://stackoverflow.com/questions/796 ... de-jupyter