rany2 / edge-tts

Use Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API key
https://pypi.org/project/edge-tts/
GNU General Public License v3.0
4.24k stars 447 forks source link

Pythonista (iOS) - error: Event loop is closed #163

Closed xxxAleksandrxxx closed 7 months ago

xxxAleksandrxxx commented 7 months ago

Thank you for the library! I tried a simple generation example code in Pythonista and it works but only once. On the second run it returns an error “Event loop is closed”. The code I tryed:

import asyncio
import edge_tts as et

def et_save_to_file(text, *, voice="en-GB-SoniaNeural", file="test.mp3"):
    async def wrapper():
        c = et.Communicate(text, voice)
        await c.save(file)

    loop = asyncio.get_event_loop_policy().get_event_loop()
    try:
        loop.run_until_complete(wrapper())
    finally:
        loop.close()

if __name__ == "__main__":
    et_save_to_file("Hey, this is a test!")

The error I got on the second run:

sys:1: RuntimeWarning: coroutine 'et_save_to_file.<locals>.wrapper' was never awaited

IMG_4821 IMG_4822 IMG_4824 IMG_4825 IMG_4823

Is there any way to avoid asyncio totally?

rany2 commented 7 months ago

Remove the try-finally and just do loop.run_until_complete(wrapper()) without loop.close()

xxxAleksandrxxx commented 7 months ago

It helps, thank you!