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

Threading with cobe? #16

Open inanimateobject opened 10 years ago

inanimateobject commented 10 years ago

Hi, I'm trying to use Brain.reply() from multiple threads, and I'm getting the following:

Exception in thread Thread-5:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/home/root/cobe/server.py", line 71, in run
    self.params[param] = brain.reply("")
  File "/home/root/cobe/cobe/brain.py", line 199, in reply
    pivot_set = self._filter_pivots(input_ids)
  File "/home/root/cobe/cobe/brain.py", line 332, in _filter_pivots
    filtered = self.graph.get_word_tokens(tokens)
  File "/home/root/cobe/cobe/brain.py", line 542, in get_word_tokens
    rows = self._conn.execute(q)
ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 139801400686400 and this is thread id 139801076410112

My code, for reference, is merely:

        elif form_params[param] == 1: # if checkbox is selected...
          self.params[param] = brain.reply("") # spit back a cobe reply

Each thread runs that code. All I need to do is somehow get random cobe replies into thread output. Any ideas?

pteichman commented 10 years ago

Unfortunately the SQLite bindings aren't thread safe, and as a result Brain.learn and Brain.reply always need to be called from the same thread. This is a pain, for sure.

Maybe use a pair of queue.Queue() instances for send/recv from a worker thread?