NEURON + Python thread(s) with the hoc interpreter

When Python is the interpreter, what is a good
design for the interface to the basic NEURON

Moderator: hines

Post Reply
Posts: 1
Joined: Sat May 19, 2012 12:15 pm

NEURON + Python thread(s) with the hoc interpreter

Post by giljael75 »


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

nrnpython("import time")
nrnpython("import test")

Code: Select all

import threading
import datetime
import time

class ThreadTest(threading.Thread):
   total = 0
   def run(self):
      while True:
        now = += 1
        print "%s : I'm working at : %s %d" % (self.getName(), now,
t = ThreadTest()
s = ThreadTest()
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.
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:

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.
Site Admin
Posts: 1601
Joined: Wed May 18, 2005 3:32 pm

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

Post 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 to

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