Сейчас использую базу данных для бота таким образом:
bot.py
:
import somelib
import sqlite3
client = somelib.Client()
client.login(email=<email>, password=<password>)
sub_client = somelib.SubClient(comId=<comId>, profile=client.profile)
db = sqlite3.connect("databases/chat1.db", check_same_thread=False)
sql = db.cursor()
old = []
class Bot:
def run(self, chatid):
commands = Commands()
title = sub_client.get_chat_thread(chatId=chatid).title
while True:
msg = sub_client.get_chat_messages(chatId=chatid, size=3)
for message, messageId, messageType, author, authorid in zip(msg.content, msg.messageId, msg.type, msg.author, msg.author.userId):
if messageId in old:
pass
else:
print(f"{title} | {author} : {message}")
if messageType == 0 and message[0] == "!":
if message.lower() == "!help":
commands.help(chatid)
if message.lower() == "!flip":
commands.flip(chatid)
if message.lower() == "!top":
commands.top(chatid, sql)
if message.lower() == "!stats":
commands.stats(authorid, author, chatid)
old.append(messageId)
main.py
:
import concurrent.futures
from lib.bot import Bot
bot = Bot(<email>, <password>)
chatids = ["aa1b9614-61df-4d3a-ac4b-a105ee75283c", "a5dd10aa-551d-41b9-95f6-96fd3bf087dc", "ed2041bf-40f1-465c-a9d9-4fd91164711f", "4496e3ac-1445-4b8b-80a9-b228534474d4", "e3b6c87a-d253-45d5-8718-365451995709", "990619b3-47ab-43ad-9f41-25f380c46e33", "eceed50e-1fda-4a55-923e-e291a6c8ecc2"]
if __name__ == '__main__':
print("START")
with concurrent.futures.ThreadPoolExecutor() as executor:
read_chats = [executor.submit(bot.run, chatid) for chatid in chatids]
Функционала и применения бд намного больше, в примере выше лишь часть кода для наглядности. Иногда вылетает программа с ошибкой Process finished with exit code -1073741819 (0xC0000005)
, судя по всему из-за высокой нагрузки на бд. Как сделать её более устойчивой к многопоточности?
sqlite3
это и есть питоновская обёртка надSQLite
:)