telebot 在使用 multiprocessing [exe 文件] 时出现错误 409
Error 409 occurred in telebot when using multiprocessing [exe file]
我想启动另一个进程,它会启动,如果我写电报机器人“启动”。但就在这发生之后错误:
2022-04-11 11:16:13,602 (__init__.py:688 Thread-1)
ERROR - TeleBot: "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"
我运行同时只有一个机器人,已经检查过了。我发现,当程序试图启动一个新进程时会发生错误。有谁能帮帮我吗?
from time import sleep
from datetime import datetime, timedelta
import telebot
from telebot import types
from threading import Thread
import multiprocessing
def collecting(a):
while True:
print(a)
sleep(3)
if __name__=='__main__':
bot = telebot.TeleBot(token)
workers = []
def listen():
@bot.message_handler(content_types='text')
def get_text(message):
id = message.chat.id
mes = message.text
if mes=='start':
bot.send_message(id, 'okey')
workers.append([str(datetime.now())])
else:
bot.send_message(id, mes)
bot.polling()
Thread(target=listen).start()
while True:
sleep(1)
x = workers[:]
for i in range(len(x)):
multiprocessing.Process(target=collecting, args=(x[i],)).start()
del workers[i]
UPD: 忘了说了,这个错误只发生在我创建exe文件和运行这段代码像exe程序的时候。这就是我创建 exe 文件的方式:
pyinstaller --hidden-import=pkg_resources.py2_warn --onefile Program.py
已解决,我忘了添加:
from multiprocessing import freeze_support
然后在if__name__=="__main__":
之后还需要写freeze_support()
.
我想启动另一个进程,它会启动,如果我写电报机器人“启动”。但就在这发生之后错误:
2022-04-11 11:16:13,602 (__init__.py:688 Thread-1)
ERROR - TeleBot: "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"
我运行同时只有一个机器人,已经检查过了。我发现,当程序试图启动一个新进程时会发生错误。有谁能帮帮我吗?
from time import sleep
from datetime import datetime, timedelta
import telebot
from telebot import types
from threading import Thread
import multiprocessing
def collecting(a):
while True:
print(a)
sleep(3)
if __name__=='__main__':
bot = telebot.TeleBot(token)
workers = []
def listen():
@bot.message_handler(content_types='text')
def get_text(message):
id = message.chat.id
mes = message.text
if mes=='start':
bot.send_message(id, 'okey')
workers.append([str(datetime.now())])
else:
bot.send_message(id, mes)
bot.polling()
Thread(target=listen).start()
while True:
sleep(1)
x = workers[:]
for i in range(len(x)):
multiprocessing.Process(target=collecting, args=(x[i],)).start()
del workers[i]
UPD: 忘了说了,这个错误只发生在我创建exe文件和运行这段代码像exe程序的时候。这就是我创建 exe 文件的方式:
pyinstaller --hidden-import=pkg_resources.py2_warn --onefile Program.py
已解决,我忘了添加:
from multiprocessing import freeze_support
然后在if__name__=="__main__":
之后还需要写freeze_support()
.