2

I am trying to run my python-telegram-bot code multiple times with different settings/options. what is the best way to do this?

this code that i am working on is sort of an infusion of django to python-telegram-bot to use it's phenomenal ORM. I am trying to run multiple instances of my bot code with different tokens. I have read about subprocess and threading and still confused about what should I do? should I even write a seprate python script and run it with the desired options/settings as arguments(with subprocess.run or os.system)?

This is going to be used in a webservice and is expected to run telegram bot instances as users need. so maybe 100 instances? it is desired to use the least cpu and memory.

ps: if there is a better title for this question suggest in comments please.

3
  • I used an API to run my python script for more than one instance using subprocess. So far no issues. Commented May 2, 2019 at 18:59
  • It would help to know why you want to run it multiple times. Since you mention subprocess and threading, I think you are looking for concurrency. But since you mention running it with different settings/options, it does not sound like you are doing that to maximize performance. Is that right? Commented May 2, 2019 at 22:04
  • @AaronBentley I's the same code base for telegram bots that is going to be run with different tokens and names etc. and maximizing performance is much desired. Commented May 3, 2019 at 18:22

1 Answer 1

3

Okay, if you're looking for maximum compute performance, you want to use subprocesses, because the Global Interpreter Lock prevents compute-intensive code from running simultaneously. (Threading and Async are good for IO-intensive scenarios.)

I suggest using the multiprocessing module for this-- it allows you to invoke subprocesses using native Python constructs instead of invoking fork / exec or worrying about which binary to execute.

Cribbing heavily from the Python docs:

from multiprocessing import Process

def bot(token):
    print(token)

if __name__ == '__main__':
    p1 = Process(target=bot, args=('token1',))
    p2 = Process(target=bot, args=('token2',))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
Sign up to request clarification or add additional context in comments.

2 Comments

My django codebase(website) is run by mod_wsgi which is multi-threaded itself, python-telegram-bot is multi-threaded too, and dose IO blocking tasks(network), my confusion comes from the sense that running the bot as a process which will take 8MB of RAM. is still subprocess the best options?
If you're IO-bound, then threading can be a viable option. Threads share memory (and other resources), which saves memory at the cost of making programming harder. There's no "best" here, just different trade-offs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.