Page 1 of 1

NEURON + Python thread(s) with the hoc interpreter

Posted: Sat May 19, 2012 1:32 pm
by giljael75
Hello!

I'm trying to generate python thread(s) in NEURON in order to communicate to a model directly.
My working environment is
NEURON version 7.3 (586:68e542bbe5b7)
Python 2.7.1+ (r271:86832)
Linux 2.6.35-22 (Ubuntu SMP)

I made a simple hoc file and a python file:

Code: Select all

//test.hoc
nrnpython("import time")
nrnpython("import test")

Code: Select all

//test.py
import threading
import datetime
import time

class ThreadTest(threading.Thread):
   total = 0
   def run(self):
      while True:
        now = datetime.datetime.now()
        self.total += 1
        print "%s : I'm working at : %s %d" % (self.getName(), now, self.total)
        time.sleep(3)
t = ThreadTest()
s = ThreadTest()
t.start()
s.start()
First, I ran the codes above with the python interpreter.
$nrniv -python
>>>import test

It worked with no problem. Two threads printed messages continuously.

Then, I ran the codes with the hoc interpreter.
$nrniv
oc>nrnpython("import test")

Two threads printed their messages once. After then, they didn't seem to want to do anything. I fed the following:
oc>nrnpython("time.sleep(1)")

They printed only once again. So I tried to terminate NEURON with quit(). Then the threads started to work normally. But I couldn't return back to the prompt (oc>) of hoc interpreter. Finally, I had to use Ctrl + 'z' to escape.

Does anybody know how to run python threads with the hoc interpreter?

Thanks in advance.

Re: NEURON + Python thread(s) with the hoc interpreter

Posted: Sun Jul 08, 2012 4:08 pm
by hines
Sorry I have not replied in a timely fashion. The question is probably stale by now.
The HOC interpreter is not threadsafe since it has no facility analogous to the GIL (global interpreter lock)
in Python. So it is important to not have two threads execute a hoc statement at the same time.
I don't know much about how Python manages the GIL so it may be the case that python itself never calls
the hoc interpreter simultaneously from different threads. In the past, when one launched nrniv -python, it was important not to execute
from neuron import gui
or else experience a segmentation violation.
When one lauched python, that statement was essential since the python readline was in control
and we needed to start a thread which every 0.1 second looked ran the InterViews event loop.
That problem is now taken care of in the latest repository version since import neuron.gui now can figure
out if the launch was from nrniv or python and decide whether to start a thread or not.

So, when in under hoc control, python threads must be stuck waiting for the GIL which hoc never releases. Your threads execute normally when
python has control as in
oc> nrnpython("threadtest.time.sleep(20)")
(I renamed test.py to threadtest.py)

At some point in the future, I need to reach an understanding of how Python handles threads and do something analogous in HOC.