У меня есть класс BotManager, который запускает новый процесс для каждого AsyncTelebot. У него есть методы:
Код: Выделить всё
async def add_bot(self, api_key: str):
if api_key not in self.bots:
if await self._check_bot(api_key):
bot = AsyncTeleBot(api_key)
self.bots[api_key] = bot
terminate_flag = multiprocessing.Event()
self.terminate_flags[api_key] = terminate_flag
process = multiprocessing.Process(target=self.bot_polling_process,
args=(api_key, terminate_flag),
daemon=True)
self.processes[api_key] = process
process.start()
else:
raise InvalidBotKeyException('Неверный API ключ для бота')
else:
raise BotAlreadyWorksException('Такой бот уже работает')
@staticmethod
def bot_polling_process(api_key, terminate_flag):
bot = AsyncTeleBot(api_key)
setup_handlers(bot)
async def polling():
while not terminate_flag.is_set():
try:
await bot.infinity_polling(timeout=10, request_timeout=90, interval=0)
except Exception as e:
print(f"Exception occurred: {e}")
continue
asyncio.run(polling())
Код: Выделить всё
2024-07-29 17:17:32,856 (asyncio_helper.py:103 MainThread) ERROR - TeleBot: "Unknown error: RuntimeError"
backend | 2024-07-29 17:17:32,857 (async_telebot.py:370 MainThread) ERROR - TeleBot: "Infinity polling exception: Request timeout. Request: method=get url=getMe params= files=None request_timeout=300"
backend | 2024-07-29 17:17:32,859 (async_telebot.py:372 MainThread) ERROR - TeleBot: "Exception traceback:
backend | Traceback (most recent call last):
backend | File "/usr/local/lib/python3.10/site-packages/telebot/async_telebot.py", line 366, in infinity_polling
backend | await self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout,
backend | File "/usr/local/lib/python3.10/site-packages/telebot/async_telebot.py", line 423, in _process_polling
backend | self._user = await self.get_me()
backend | File "/usr/local/lib/python3.10/site-packages/telebot/async_telebot.py", line 2579, in get_me
backend | result = await asyncio_helper.get_me(self.token)
backend | File "/usr/local/lib/python3.10/site-packages/telebot/asyncio_helper.py", line 154, in get_me
backend | return await _process_request(token, method_url)
backend | File "/usr/local/lib/python3.10/site-packages/telebot/asyncio_helper.py", line 105, in _process_request
backend | raise RequestTimeout("Request timeout. Request: method={0} url={1} params={2} files={3} request_timeout={4}".format(method, url, params, files, request_timeout, current_try))
backend | telebot.asyncio_helper.RequestTimeout: Request timeout. Request: method=get url=getMe params= files=None request_timeout=300
backend | "
Мой Dockerfile
Код: Выделить всё
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Код: Выделить всё
version: '3.8'
networks:
dev:
services:
mongo:
image: mongo:latest
container_name: mongo
environment:
- MONGO_INITDB_DATABASE=telegram_posts
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db
networks:
- dev
redis:
image: redis:latest
container_name: redis
ports:
- "6379:6379"
networks:
- dev
backend:
build:
context: ./telePosting
dockerfile: Dockerfile
container_name: backend
environment:
- MONGO_URL=mongodb://mongo:27017
- MONGO_DB_NAME=telegram_posts
- REDIS_URL=redis
- REDIS_PORT=6379
- PYTHONUNBUFFERED=1
ports:
- "8000:8000"
depends_on:
- mongo
- redis
networks:
- dev
frontend:
build:
context: ./telePostFrontend/vite-project
dockerfile: Dockerfile
container_name: frontend
ports:
- "3000:80"
depends_on:
- backend
networks:
- dev
volumes:
mongo-data:
Я попытался выполнить проверку связи с некоторыми сайтами и обнаружил, что другие процессы не имеют подключения к Интернету.
Подробнее здесь: https://stackoverflow.com/questions/788 ... the-docker