Reputation: 109
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.
Upvotes: 2
Views: 2152
Reputation: 1380
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()
Upvotes: 3