TG bot in a separate thread... again. #4167
-
Hi guys, I know this has been asked before, in a slightly modified way, but I was unable to reproduce this as working code. API changes did not make things easier, since it's often unclear to me which examples still apply and which are outdated. I could also need some help in picking the right abstraction layer (raw telegram vs. convenience-wrappers). My project consists of multiple i/o interfaces and independently running tasks (some of which call external, long running Linux binaries), and makes heavy use of multi threading, with message queues ("import queue") for communication between threads. Every one of my worker threads has an input message queue. Everyhting works nicely... except for the TG interface. The code of the Telegram thread in in essence (somewhat pseudo-code-ish):
And when a message is received from Telegram, the TG bot handler should call The bot does not need to do fancy stuff, the only things I need to do are:
How do I do this best with python-telegram-bot? EDIT, side note: I've read all the discussion threads on the python-telegram-bot.org page on PTB vs. threads, and on stackoverflow as well. There has been hinting (?) that run_polling needs to be split into discrete calls, but I'm not sure if that info is still valid with the current API, or if it is even related to threads. I have a growing feeling that in general PTB can't be run in a thread that's different from the main thread, but have not found definitive info about this, since where threads are mentioned it's only on a very abstract level (using additional frameworks etc). So, is this possible? If no, what would be a way to work around this? Would moving PTB into its own process (multiprocessing) be an option? Any suggestions are very much appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Beta Was this translation helpful? Give feedback.
-
The content of
is always the latest stable status. Regarding running PTB in a separate thread:
Mh, this just means that PTB takes no measures that ensures thread safety when e.g. sharing PTB objects between thread. Simply b/c PTB is written for asyncio code and asyncio code is not usually multi-threaded. It does however not mean that using PTB in a multi-threaded setup is generally impossible. It's mostly a disclaimer stating that you'll have to take care of thread-safety yourself :)
I guess there are two main points here:
I'm aware that my answer doesn't give you one clear path to go. I hope that I could at least clarify some general points that lead you into the right direction and help you figure out a setup that suits your needs. Feel free to ask any follow-up questions :) |
Beta Was this translation helpful? Give feedback.
-
As a reference for other users, I found a way to start the bot in a non-main thread. Here's some code snippet:
In the asyncio loop, you can asyncio-read from stdin, grab messages form queues etc... |
Beta Was this translation helpful? Give feedback.
The content of
is always the latest stable status.
Regarding running PTB in a separate thread:
There is nothing inheritedly stopping you from doing that. you might need to create an event loop in the non-main-thread, but otherwise it should be possible to e.g. simply run echobot in that non-main-thread. This is in fact rather a general Python question and not specific to PTB.