RuntimeError: Event Loop Already Running in Telegram Bot with asyncio and python-telegram-bot

I’m developing a Telegram bot that suggests movies using the python-telegram-bot library and IMDbPY. I switched from synchronous to asynchronous code using asyncio to handle waiting times more efficiently, but I’m encountering the following error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>RuntimeError: This event loop is already running
</code>
<code>RuntimeError: This event loop is already running </code>
RuntimeError: This event loop is already running

Additionally, another error occurs when attempting to close the event loop:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>RuntimeError: Cannot close a running event loop
</code>
<code>RuntimeError: Cannot close a running event loop </code>
RuntimeError: Cannot close a running event loop

I’m using Python 3.12 and asyncio.run(main()) to start the bot. I’m fairly new to asynchronous programming, so I’m not entirely sure how to handle the event loop properly in this context.

The error occurs when I try to run the bot using await application.run_polling(). My main function is set up with asyncio.run(main()). Here’s a simplified version of my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import asyncio
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters
from imdb import IMDb
async def main():
application = ApplicationBuilder().token("YOUR_BOT_TOKEN").build()
# Handlers
application.add_handler(CommandHandler("start", init))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, genres_selected))
await application.run_polling() # Issue occurs here
await application.idle()
if __name__ == '__main__':
asyncio.run(main())
</code>
<code>import asyncio from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters from imdb import IMDb async def main(): application = ApplicationBuilder().token("YOUR_BOT_TOKEN").build() # Handlers application.add_handler(CommandHandler("start", init)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, genres_selected)) await application.run_polling() # Issue occurs here await application.idle() if __name__ == '__main__': asyncio.run(main()) </code>
import asyncio
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters
from imdb import IMDb

async def main():
    application = ApplicationBuilder().token("YOUR_BOT_TOKEN").build()

    # Handlers
    application.add_handler(CommandHandler("start", init))
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, genres_selected))

    await application.run_polling()  # Issue occurs here
    await application.idle()

if __name__ == '__main__':
    asyncio.run(main())

I understand that asyncio.run() creates its own event loop, but I’m unsure how to manage this since ApplicationBuilder uses asyncio as well.

How can I properly run the bot without encountering this error? Should I avoid using asyncio.run() with run_polling(), or is there a better approach to handle async tasks in this situation?

Any insights would be greatly appreciated!

I tried switching from start_polling() to run_polling() because I thought this might solve the issue, but the error persisted. I expected the bot to run normally and start polling for messages, but instead, I received the error about the event loop running multiple times.

I’ve looked into solutions like using asyncio.get_event_loop() but I’m still running into problems with the event loop management.

run_polling is not a coroutine function and it should not be used within a coroutine function. Moreover, there is no method Application.idle() – this looks like the updater.idle() code that was used in python-telegram-bot versions 13 and older.

Make your main function a standard, non-coroutine function and call it without asyncio.run. Remove the await in front of application.run_polling. THat should do the trick.

I strongly recommend reading the official resources provided by the PTB team to get familiar with the library.


Disclaimer: I’m currently the maintainer of python-telegram-bot.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật