I’m working on a Python Telegram bot using the Telethon library and I’m facing an issue with handling multiple clients.
Right now my bot works with just one client connection (telegram_bot). However, I need it to support multiple clients where the number isn’t predetermined. When users provide their login details, the bot should automatically create a new client instance for their account and start monitoring it.
The problem is that once I call telegram_bot.run_until_disconnected(), the script keeps running and I can’t figure out how to add more clients dynamically. I tried using threading but couldn’t get it working properly for an unknown number of clients that keeps changing.
You need to completely restructure this. Don’t use run_until_disconnected() - build a custom event loop that keeps control while handling multiple clients. I ran into this same issue building a multi-account crypto scraper. The trick is treating client creation as a service in your main app, not separate processes. Make a ClientManager class with an active clients registry. When new credentials come in, create the TelegramClient but skip run_until_disconnected(). Register the event handlers dynamically and add it to your event loop with asyncio.ensure_future(). Your main loop becomes a basic while True with await asyncio.sleep() calls - all registered clients process events concurrently. The tricky bit is session management. Store each client’s StringSession separately and handle cleanup when clients disconnect. You’ll get fine control over each connection without threading headaches.
just use a client pool with asyncio.gather() - way easier than handling tasks yourself. set up all your telethon clients at the start but skip calling run_until_disconnected. instead, create coroutines for each client’s message loop and dump them all into asyncio.gather(). when someone new signs up, toss their client coroutine into the pool and restart gather() with the new list. it’s a bit hacky but beats dealing with threading hell and scales nicely.
Ditch run_until_disconnected() and use asyncio task management instead. Keep a dictionary of active clients and spawn each one with asyncio.create_task() so they run independently. I built a client manager class that tracks all connections. When someone gives new credentials, it creates a TelegramClient instance and spawns an async task - basically client.start() then client.run_until_disconnected() wrapped in a task. Run your main event loop separately with asyncio.run() and a simple keepalive function. New clients get added to the task pool on the fly. Each client handles its own messages and disconnects without blocking the others. For cleanup, keep references to all tasks so you can cancel them when users disconnect. This stops memory leaks and cleans up sessions properly. The tricky bit is exception handling - wrap each client task in try-catch blocks or one failing client will crash everything.
Don’t run multiple Telethon clients in the same Python process. You’ll waste more time debugging connection states and memory issues than actually building stuff.
I learned this managing a system with 50+ Telegram accounts. Started with the asyncio task approach everyone recommends, but it’s a nightmare when clients randomly disconnect or hit rate limits.
Treat each client as its own service instead. Don’t cram everything into one Python script - build a workflow that manages client lifecycle externally.
When someone submits credentials, trigger a workflow that validates login, creates a dedicated client instance, and monitors health. Each client runs isolated so failures don’t cascade.
The workflow handles reconnections, session management, scaling with new users, and cleaning up dead connections. Your main bot just handles user interactions and delegates the heavy work.
This scales way better than juggling async tasks in memory. Plus you get monitoring and can easily add features like automatic failover.
Latenode handles this pattern really well. You can build workflows that spin up isolated Telegram clients dynamically and manage their lifecycle without touching your bot code.
Managing multiple Telethon clients dynamically is a pain - you’re juggling async loops and client lifecycles. Threading with Telethon gets messy fast, especially when you don’t know how many clients you’ll need.
I hit this exact problem building a monitoring system for dozens of Telegram accounts. The usual approach of managing multiple async clients in one Python process becomes a nightmare with memory leaks and connection issues.
What worked for me? Move this logic outside your app entirely. Don’t try managing multiple clients in your Python code - set up an automation workflow that handles each client as a separate process or container.
The workflow monitors for new user registrations, spins up isolated Telethon instances for each account, and manages their lifecycles independently. When a client disconnects or fails, it restarts just that instance without affecting others.
This gives you better scalability since each client runs isolated, and you can easily scale horizontally with hundreds of accounts.
Latenode makes this dynamic client management really straightforward with workflow automation. You can set up triggers for new registrations and automatically provision new Telegram client instances without touching your main bot code.