diff --git a/pyrogram/client.py b/pyrogram/client.py index 1e53e8f6..9e603c73 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -380,10 +380,7 @@ class Client(Methods): if isinstance(loop, asyncio.AbstractEventLoop): self.loop = loop else: - try: - self.loop = asyncio.get_running_loop() - except RuntimeError: - self.loop = asyncio.new_event_loop() + loop = asyncio.get_event_loop() def __enter__(self): return self.start() diff --git a/pyrogram/connection/connection.py b/pyrogram/connection/connection.py index a7f6acdb..016907e4 100644 --- a/pyrogram/connection/connection.py +++ b/pyrogram/connection/connection.py @@ -55,10 +55,7 @@ class Connection: if isinstance(loop, asyncio.AbstractEventLoop): self.loop = loop else: - try: - self.loop = asyncio.get_running_loop() - except RuntimeError: - self.loop = asyncio.new_event_loop() + self.loop = asyncio.get_event_loop() async def connect(self) -> None: for _ in range(Connection.MAX_CONNECTION_ATTEMPTS): diff --git a/pyrogram/connection/transport/tcp/tcp.py b/pyrogram/connection/transport/tcp/tcp.py index fcfbe6ad..13e9f806 100644 --- a/pyrogram/connection/transport/tcp/tcp.py +++ b/pyrogram/connection/transport/tcp/tcp.py @@ -58,10 +58,7 @@ class TCP: if isinstance(loop, asyncio.AbstractEventLoop): self.loop = loop else: - try: - self.loop = asyncio.get_running_loop() - except RuntimeError: - self.loop = asyncio.new_event_loop() + self.loop = asyncio.get_event_loop() async def _connect_via_proxy( self, diff --git a/pyrogram/methods/auth/initialize.py b/pyrogram/methods/auth/initialize.py index f08778fd..a7484585 100644 --- a/pyrogram/methods/auth/initialize.py +++ b/pyrogram/methods/auth/initialize.py @@ -49,6 +49,6 @@ class Initialize: await self.dispatcher.start() - self.updates_watchdog_task = asyncio.create_task(self.updates_watchdog()) + self.updates_watchdog_task = self.loop.create_task(self.updates_watchdog()) self.is_initialized = True diff --git a/pyrogram/methods/utilities/run.py b/pyrogram/methods/utilities/run.py index addb2202..ab1adc5f 100644 --- a/pyrogram/methods/utilities/run.py +++ b/pyrogram/methods/utilities/run.py @@ -71,8 +71,7 @@ class Run: app.run(main()) """ - loop = asyncio.get_event_loop() - run = loop.run_until_complete + run = self.loop.run_until_complete if coroutine is not None: run(coroutine) diff --git a/pyrogram/utils.py b/pyrogram/utils.py index fca5dbb6..1baf5a2f 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -50,10 +50,7 @@ async def ainput(prompt: str = "", *, hide: bool = False, loop: Optional[asyncio if isinstance(loop, asyncio.AbstractEventLoop): loop = loop else: - try: - loop = asyncio.get_running_loop() - except RuntimeError: - loop = asyncio.new_event_loop() + loop = asyncio.get_event_loop() with ThreadPoolExecutor(1) as executor: func = functools.partial(getpass if hide else input, prompt)