pteichman / cobe

A Markov chain based text generation library and MegaHAL style chatbot
http://teichman.org/blog/
MIT License
239 stars 51 forks source link

_sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140491086620544 and this is thread id 140490980185856 #38

Open ronilaukkarinen opened 3 years ago

ronilaukkarinen commented 3 years ago

Trying to implement cobe as sopel module.

Getting:

[2021-05-22 17:06:27] ERROR - Unexpected error (SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140491086620544 and this is thread id 140490980185856) from rolle at 2021-05-22 17:06:27.386482. Message was: kummitus: Päivää
Traceback (most recent call last):
  File "/usr/local/lib/pypy3.6/dist-packages/sopel/bot.py", line 606, in call
    exit_code = func(sopel, trigger)
  File "/home/rolle/.sopel/modules/megahal.py", line 36, in talkbot
    response = b.reply(request)
  File "/usr/local/lib/pypy3.6/dist-packages/cobe/brain.py", line 205, in reply
    input_ids = list(map(self.graph.get_token_by_text, tokens))
  File "/usr/local/lib/pypy3.6/dist-packages/cobe/brain.py", line 525, in get_token_by_text
    c = self.cursor()
  File "/usr/local/lib/pypy3.6/dist-packages/cobe/brain.py", line 466, in cursor
    return self._conn.cursor()
  File "/usr/lib/pypy3/lib_pypy/_sqlite3.py", line 407, in cursor
    self._check_thread()
  File "/usr/lib/pypy3/lib_pypy/_sqlite3.py", line 312, in _check_thread
    "is thread id %d" % (self.__thread_ident, threading.get_ident()))

Module source:

"""
megahal.py - MegaHAL for sopel IRC bot
Copyright 2021, Roni Laukkarinen [roni@dude.fi]"
Licensed under the WTFPL. Do whatever the fuck you want with this. You just
  can't hold me responsible if it breaks something either.
A module for the Sopel IRC Bots.
"""

from cobe.brain import Brain
import sopel.module

b = Brain("cobe.brain")
b.learn('./trainerfile.txt')

# Learn everything (for some reason this regex causes problems when someone says ":(" for example):
@sopel.module.rule(".*")

def megahal_all(bot, trigger):
    only_message_all_check_only = trigger.split(": ", 1)

    if len(only_message_all_check_only) >= 2 and only_message_all_check_only[1]:
      only_message_all = trigger.split(": ", 1)[1]
      b.learn

@sopel.module.nickname_commands(".*")

def megahal(bot, trigger):

    only_message_check_only = trigger.split(": ", 1)

    if len(only_message_check_only) >= 2 and only_message_check_only[1]:
      query = trigger.replace('!', '')
      only_message = query.split(": ", 1)[1]

      request = only_message
      response = b.reply(request)
      bot.reply(response)

I'm not very experienced in python so all tips appreciated.