Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PTB on Jupyter Notebooks and Google Collab #3251

Closed
husanpy opened this issue Sep 20, 2022 · 3 comments
Closed

PTB on Jupyter Notebooks and Google Collab #3251

husanpy opened this issue Sep 20, 2022 · 3 comments

Comments

@husanpy
Copy link

husanpy commented Sep 20, 2022

Issue I am facing

I am trying to run my first bot from Tutorial in Jupyter Notebook and facing this issue:

RuntimeError: Cannot close a running event loop

Though when running on Ubuntu WSL it perfectly works! But Jupyter is way more comfy for me than creating and running files in Ubuntu, so I have a question how to fix this.

Traceback to the issue

RuntimeError                              Traceback (most recent call last)
~\anaconda3\lib\site-packages\telegram\ext\_application.py in __run(self, updater_coroutine, stop_signals, close_loop)
    792             updater_coroutine.close()
--> 793             raise exc
    794         finally:

~\anaconda3\lib\site-packages\telegram\ext\_application.py in __run(self, updater_coroutine, stop_signals, close_loop)
    781         try:
--> 782             loop.run_until_complete(self.initialize())
    783             if self.post_init:

~\anaconda3\lib\asyncio\base_events.py in run_until_complete(self, future)
    617         self._check_closed()
--> 618         self._check_running()
    619 

~\anaconda3\lib\asyncio\base_events.py in _check_running(self)
    577         if self.is_running():
--> 578             raise RuntimeError('This event loop is already running')
    579         if events._get_running_loop() is not None:

RuntimeError: This event loop is already running

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
~\anaconda3\lib\site-packages\telegram\ext\_application.py in __run(self, updater_coroutine, stop_signals, close_loop)
    801                     loop.run_until_complete(self.stop())
--> 802                 loop.run_until_complete(self.shutdown())
    803                 loop.run_until_complete(self.updater.shutdown())  # type: ignore[union-attr]

~\anaconda3\lib\asyncio\base_events.py in run_until_complete(self, future)
    617         self._check_closed()
--> 618         self._check_running()
    619 

~\anaconda3\lib\asyncio\base_events.py in _check_running(self)
    577         if self.is_running():
--> 578             raise RuntimeError('This event loop is already running')
    579         if events._get_running_loop() is not None:

RuntimeError: This event loop is already running

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11840/1763958347.py in <module>
      4 application.add_handler(start_handler)
      5 
----> 6 application.run_polling()

~\anaconda3\lib\site-packages\telegram\ext\_application.py in run_polling(self, poll_interval, timeout, bootstrap_retries, read_timeout, write_timeout, connect_timeout, pool_timeout, allowed_updates, drop_pending_updates, close_loop, stop_signals)
    631             self.create_task(self.process_error(error=exc, update=None))
    632 
--> 633         return self.__run(
    634             updater_coroutine=self.updater.start_polling(
    635                 poll_interval=poll_interval,

~\anaconda3\lib\site-packages\telegram\ext\_application.py in __run(self, updater_coroutine, stop_signals, close_loop)
    804             finally:
    805                 if close_loop:
--> 806                     loop.close()
    807 
    808     def create_task(self, coroutine: Coroutine, update: object = None) -> asyncio.Task:

~\anaconda3\lib\asyncio\selector_events.py in close(self)
     87     def close(self):
     88         if self.is_running():
---> 89             raise RuntimeError("Cannot close a running event loop")
     90         if self.is_closed():
     91             return

RuntimeError: Cannot close a running event loop

Related part of your code

if __name__ == '__main__':
    application = ApplicationBuilder().token('TOKEN').build()
    
    start_handler = CommandHandler('start', start)
    application.add_handler(start_handler)
    
    application.run_polling()

Operating System

Windows 10

Version of Python, python-telegram-bot & dependencies

python-telegram-bot 20.0a1
Bot API 6.0
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)]
@Bibo-Joshi Bibo-Joshi changed the title [What is the reason for the following error? "RuntimeError: Cannot close a running event loop" ] What is the reason for the following error? "RuntimeError: Cannot close a running event loop" Sep 20, 2022
@Bibo-Joshi
Copy link
Member

Hi. Thanks for reaching out.

AFAIK, jupyter notebooks to a lot of background magic to make asyncio code easily usable. As side effect, one apparently cannot directly use loop.run_until_complete directly, which is what Application.run_{polling, webhook} do internally.

You might be able to work around this directly by using the methods that Application.run_polling is a convenience method for. Feel free to check out the source code for all the details on what run_polling does.

TBH, as a maintainer, I don't see a priority of trying to natively support running PTB in jupyter notebooks. For one, I'm not very familiar with those and to the best of my knowledge jupyter notebooks are not really designed to execute long-running processes. Secondly, I'm not quite sure how PTB could/should/would do things differently to work in jupyter notebooks more easily. If you have any suggestions on what PTB could improve, you are of course very welcome to share your findings with us so that we can see if we can adapt them!

@Bibo-Joshi Bibo-Joshi changed the title What is the reason for the following error? "RuntimeError: Cannot close a running event loop" PTB on Jupyter Notebooks Sep 20, 2022
@husanpy
Copy link
Author

husanpy commented Sep 21, 2022

@Bibo-Joshi , thank you for your kind reply!

I got you! I just downloaded PyCharm and started to code there

@Deadfish-hk
Copy link

For those who are using jupyter notebook or google colab for python-telegram bot >20.0 ,
you can try using nest_asyncio as a workaround to loop.run_until_complete.

@Bibo-Joshi Bibo-Joshi changed the title PTB on Jupyter Notebooks PTB on Jupyter Notebooks and Google Collab Mar 30, 2023
@Bibo-Joshi Bibo-Joshi closed this as not planned Won't fix, can't repro, duplicate, stale Jul 28, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Aug 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants