1818# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
1919
2020import asyncio
21- import inspect
2221from typing import TYPE_CHECKING
2322
2423from hydrogram .methods .utilities .idle import idle
@@ -32,12 +31,12 @@ def run(self: "hydrogram.Client", coroutine=None):
3231 """Start the client, idle the main script and finally stop the client.
3332
3433 When calling this method without any argument it acts as a convenience method that calls
35- :meth:`~hydrogram.Client.start`, :meth:`~hydrogram.idle` and :meth:`~hydrogram.Client.stop` in sequence.
36- It makes running a single client less verbose.
34+ :meth:`~hydrogram.Client.start`, :meth:`~hydrogram.idle` and :meth:`~hydrogram.Client.stop`
35+ in sequence. It makes running a single client less verbose.
3736
38- In case a coroutine is passed as argument, runs the coroutine until it's completed and doesn't do any client
39- operation. This is almost the same as :py:obj:`asyncio.run` except for the fact that Hydrogram's ``run`` uses the
40- current event loop instead of a new one.
37+ In case a coroutine is passed as argument, runs the coroutine until it's completed and
38+ doesn't do any client operation. This is almost the same as :py:obj:`asyncio.run` except
39+ for the fact that Hydrogram's ``run`` uses the current event loop instead of a new one.
4140
4241 If you want to run multiple clients at once, see :meth:`hydrogram.compose`.
4342
@@ -76,11 +75,14 @@ async def main():
7675
7776 if coroutine is not None :
7877 run (coroutine )
79- elif inspect .iscoroutinefunction (self .start ):
78+ else :
79+ if loop .is_running ():
80+ raise RuntimeError (
81+ "You must call client.run() method outside of an asyncio event loop. "
82+ "Otherwise, you can use client.start(), client.idle(), and client.stop() "
83+ "methods. Refer to https://docs.hydrogram.org/en/latest/api/methods/run.html"
84+ )
85+
8086 run (self .start ())
8187 run (idle ())
8288 run (self .stop ())
83- else :
84- self .start ()
85- run (idle ())
86- self .stop ()
0 commit comments