1

Решил уместить функционал нескольких ботов в один файл, так как уже много их в папке. Но проблема что из всех ботов запускается только первый в

if __name__ == '__main__':
    while True:
        try:
            bot1.polling(non_stop=True, interval=0, timeout=60)
            bot2.polling(non_stop=True, interval=0, timeout=60)
            bot3.polling(non_stop=True, interval=0, timeout=60)
        except Exception as e:
            time.sleep(2)

Запускается только bot1, функционал других не работает, как это исправить и сделать запуск сразу нескольких ботов, или это невозможно на telebot и надо переходить на aiogram? Пробовал реализацию через threading:

def bot1polling():
    bot1.polling(non_stop=True, interval=0)
def bot2polling():
    bot2.polling(non_stop=True, interval=0)
def bot2polling():
    bot3.polling(non_stop=True, interval=0)
if __name__ == '__main__':
    while True:
        try:
            bot1poll = Thread(target=bot1polling, daemon=True)
            bot1poll.start()  
            bot2poll = Thread(target=bot2polling, daemon=True)
            bot2poll.start()  
            bot3poll = Thread(target=bot3polling, daemon=True)
            bot3poll.start()  
        except Exception as e:
            time.sleep(2)
5
  • случаем не threading? 27 мая в 6:07
  • Я пытался через threading, но у telebot есть такая штука что он может вырубиться через пару часов из-за timeout error. Хотя может я неправильно threading реализую
    – robo.copp
    27 мая в 6:12
  • А как вы пытались через threading? Можете в вопрос поместить? 27 мая в 6:12
  • Добавил в вопрос мою реализацию через threading
    – robo.copp
    27 мая в 6:16
  • может не non_stop, а none_stop? 27 мая в 6:19

1 ответ 1

2

Попробуйте сделать так:

В начале файла:

from multiprocessing import Process

В конце файла (перезапишите код if __name__ == '__main__' этим кодом):

def start_bot1():
    bot1.polling(none_stop=True, interval=0, timeout=60)

def start_bot2():
    bot2.polling(none_stop=True, interval=0, timeout=60)

def start_bot3():
    bot3.polling(none_stop=True, interval=0, timeout=60)

if __name__ == '__main__':
    while True:
        try:
            bot_thread1 = Process(target=start_bot1, args=())
            bot_thread2 = Process(target=start_bot2, args=())
            bot_thread3 = Process(target=start_bot3, args=())

            bot_thread1.start()
            bot_thread2.start()
            bot_thread3.start()

            bot_thread1.join()
            bot_thread2.join()
            bot_thread3.join()
        except Exception as e:
            time.sleep(10)
13
  • Проблема в том что боты сходят с ума, polling каждый раз перезапускается и ответ на 1 команду может копироваться трижды.
    – robo.copp
    27 мая в 6:24
  • А вы мой код пробовали? 27 мая в 6:25
  • Это и есть последствия Вашего кода. Вот такое выходит в консоли: A request to the Telegram API was unsuccessful. Error code: 409. Description: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running (якобы бот несколько раз запущен)
    – robo.copp
    27 мая в 6:27
  • Это ошибка в вашем коде. Проверьте, чтобы были разные токены у всех ботов. 27 мая в 6:28
  • 1
    ну так у вас один токен наверное
    – ZxNuClear
    27 мая в 6:28

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.