I have a telegram bot that I want to use to start another telegram bot. You cannot run two telegram bots within a single python script, so I have two python scripts: main.py and gs_main.py. When I run them concurrently in two seperate screen sessions everything is fine. However, starting gs_main.py from main.py is causing me headaches. My last try is as follows:
In main.py I have the following command to start gs_main.py:
def gostart(update: Update, context: CallbackContext) -> None:
user = update.effective_user
global goserver
if user.id == USER_ID:
goserver = os.system("./start_goserver.sh")
The contents of start_goserver.sh are as follows:
cd /home/ubuntu/gosharing
python3 gs_main.py
However, this seems to open gs_main.py as a subprocess of main.py since I am getting the following error:
Error while getting Updates: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
No error handlers are registered, logging exception.
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/ext/updater.py", line 646, in _network_loop_retry
if not action_cb():
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/ext/updater.py", line 597, in polling_action_cb
updates = self.bot.get_updates(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/ext/extbot.py", line 222, in get_updates
updates = super().get_updates(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/bot.py", line 130, in decorator
result = func(*args, **kwargs)
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/bot.py", line 2861, in get_updates
self._post(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/bot.py", line 295, in _post
return self.request.post(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/utils/request.py", line 361, in post
result = self._request_wrapper(
File "/home/ubuntu/.local/lib/python3.8/site-packages/telegram/utils/request.py", line 283, in _request_wrapper
raise Conflict(message)
telegram.error.Conflict: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running
My question is, how do I run gs_main.py without it being a subprocess of main.py?
You could try to send your both process to background if you use linux.
If you like to run both with a simple command, you could create a third script like this:
import os
os.spawnl(os.P_DETACH, python, 'script_1.py')
os.spawnl(os.P_DETACH, python, 'script_2.py')
You could find more information about spawnl function here:
I manually decoupled the two scripts as follows. In my main.py I changed the command to this:
def gostart(update: Update, context: CallbackContext) -> None:
user = update.effective_user
global goserver
if user.id == USER_ID:
os.system("echo 1 > /home/ubuntu/gosharing/run")
Then, in /home/ubuntu/gosharing I created a second main.py with the following code:
import os
run = 0
prev = 0
while True:
file = open("run", "rb")
try:
run = int(file.readline())
except:
pass
file.close()
if (prev == 0) & (run == 1):
os.system("pkill -f gs_main.py && python3 gs_main.py > log &")
print("starting server")
if (prev == 1) & (run == 0):
os.system("pkill -f gs_main.py")
print("killing server")
prev = run
It works:). Might not be the most elegant solution but it does the trick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.