Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

oct2py - Calling an octave function using threads in python

I was trying to call an Octave function from a python program using two threads. My octave code is just to see how it works -

testOctave.m

function y = testOctave(i)
    y = i;
end

And the python program just tries to call it

from oct2py import octave
import thread
def func(threadName,i) :
    print "hello",threadName   // This printf works
    y = octave.testOctave(i)
    print y   // This is ignored
    print 'done'   // This is ignored
    print 'exiting'    // This is ignored

try:
    thread.start_new_thread( func, ("Thread-1", 100 ) )
    thread.start_new_thread( func, ("Thread-2", 150 ) )
except:
    print "Error: unable to start thread"

The program exits without giving any errors, but in the above function, the first print is only executed, all prints following the octave call are ignored by both threads. Is there a reason why this happens, and what can I do to make it work?

The program doesnt do anything in particular, Im just trying to figure out how to work with oct2py

like image 566
Avisek Avatar asked Dec 25 '22 22:12

Avisek


1 Answers

oct2py creator here. When you import octave from oct2py you are importing a convenience instance of the Oct2Py class. If you want to use multiple threads, you must import Oct2Py and instantiate it either within your threaded function or pre-allocate and pass it as an argument to the function. Each instance of Oct2Py uses its own Octave session and dedicated MAT files for I/O.

from oct2py import Oct2Py
import thread
def func(threadName,i) :
    oc = Oct2Py()
    y = oc.testOctave(i)
    print y

try:
    thread.start_new_thread( func, ("Thread-1", 100 ) )
    thread.start_new_thread( func, ("Thread-2", 150 ) )
except:
    print "Error: unable to start thread"
like image 198
Steven Silvester Avatar answered Mar 17 '23 01:03

Steven Silvester