Using oct2py : Calling an octave function using threads

This is the place for queries that don't fit in any of the other categories.

Using oct2py : Calling an octave function using threads

Postby Avisek » Thu Aug 15, 2013 4:16 pm

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
Code: Select all
function y = testOctave(i)
    y = i;
end


And the python program just tries to call it
Code: Select all
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
Avisek
 
Posts: 5
Joined: Thu Aug 15, 2013 4:12 pm

Re: Using oct2py : Calling an octave function using threads

Postby micseydel » Thu Aug 15, 2013 5:23 pm

If you start the threads outside of the blanket try/except, do you get an error message? You might be hiding some very useful information by catching and ignoring all exceptions.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1262
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Using oct2py : Calling an octave function using threads

Postby Avisek » Thu Aug 15, 2013 5:47 pm

If I remove the try/except and just start the threads, I get the same output, without any errors. That is, the first printf works for both threads, and the rest of the function seems to be ignored.
Is there a way to see line-by-line execution of a code?
Avisek
 
Posts: 5
Joined: Thu Aug 15, 2013 4:12 pm

Re: Using oct2py : Calling an octave function using threads

Postby Avisek » Thu Aug 15, 2013 5:58 pm

Wow okay, by doing a normal execution by doing a

Code: Select all
python train1.py


, it doesnt show any errors. But if I try to do a

Code: Select all
python -m trace --count -C . train1.py


Then it shows me a number of exceptions. The first time it showed me -

Code: Select all
helohelo Thread-1
 Thread-2
Unhandled exception in thread started by <function func at 0x2cf2a28>
Unhandled exception in thread started by <function func at 0x2cf2a28>
Traceback (most recent call last):
  File "train1.py", line 20, in func
    y = octave.testOctave(i)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_oct2py.py", line 378, in octave_command
Traceback (most recent call last):
    return self.call(name, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_oct2py.py", line 208, in call
  File "train1.py", line 20, in func
    return self._reader.extract_file(argout_list)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_matread.py", line 74, in extract_file
        y = octave.testOctave(i)
val = data[arg]
KeyError: 'a__'  File "/usr/local/lib/python2.7/dist-packages/oct2py/_oct2py.py", line 378, in octave_command

    return self.call(name, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_oct2py.py", line 172, in call
    self._get_dummy_cell()
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_oct2py.py", line 453, in _get_dummy_cell
    self.get('__cell')
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_oct2py.py", line 284, in get
    return self._reader.extract_file(argout_list)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_matread.py", line 71, in extract_file
    data = loadmat(self.out_file)
  File "/usr/lib/python2.7/dist-packages/scipy/io/matlab/mio.py", line 175, in loadmat
    matfile_dict = MR.get_variables(variable_names)
  File "/usr/lib/python2.7/dist-packages/scipy/io/matlab/mio5.py", line 272, in get_variables
    hdr, next_position = self.read_var_header()
  File "/usr/lib/python2.7/dist-packages/scipy/io/matlab/mio5.py", line 234, in read_var_header
    raise TypeError('Expecting miMATRIX type here, got %d' %  mdtype)
TypeError: Expecting miMATRIX type here, got 6


And all subsequent calls it shows me

Code: Select all
helohelo  Thread-2
Thread-1
150
done
exiting    // This part changes, as theyre being produced by two different threads
Unhandled exception in thread started by <function func at 0x1cf7a28>
Traceback (most recent call last):
  File "train1.py", line 20, in func
    y = octave.testOctave(i)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_oct2py.py", line 378, in octave_command
    return self.call(name, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_oct2py.py", line 172, in call
    self._get_dummy_cell()
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_oct2py.py", line 454, in _get_dummy_cell
    cell = self._reader.get_dummy_cell()
  File "/usr/local/lib/python2.7/dist-packages/oct2py/_matread.py", line 137, in get_dummy_cell
    return data['__cell'][0]
KeyError: '__cell'
Avisek
 
Posts: 5
Joined: Thu Aug 15, 2013 4:12 pm

Re: Using oct2py : Calling an octave function using threads

Postby micseydel » Thu Aug 15, 2013 6:02 pm

I can't help you with Octave, but I can give this advice: before involving threads and error handling, verify that simply calling the Octave function from Python serially is successful, and then build from there. Threads and exception handling the way you're doing it will hide very useful information from you at least some of the time.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1262
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Using oct2py : Calling an octave function using threads

Postby Avisek » Thu Aug 15, 2013 6:44 pm

If i just call Octave functions, without using threads, that seems to work fine. Using threads seems to introduce trouble. If I use a single thread, it ignores the prints following the Octave call, however when I try to trace it, it actually shows all the outputs, and it doesnt show any of the previous exceptions. It is showing the exceptions only when using two or more threads.

But thanks for the advice :D lets see if I can find anything else on oct2py.
Avisek
 
Posts: 5
Joined: Thu Aug 15, 2013 4:12 pm

Re: Using oct2py : Calling an octave function using threads

Postby micseydel » Thu Aug 15, 2013 6:55 pm

Are you sure that Octave functions in Python are thread-safe? Some functions manipulate a global state, and so running two of them in parallel introduces confusing problems.

Also, you may want to seek a forum specialized for Octave, or maybe even Octave and Python. This seems rather specialized.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1262
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Using oct2py : Calling an octave function using threads

Postby Avisek » Thu Aug 15, 2013 7:28 pm

I actually dont know if they are thread safe. The operations I'm performing should be thread-safe, but I dont know if there are other conflicts. I'll try looking for a forum for Octave, there seems to be very little on oct2py or Octave and python together. I should probably go for a different approach altogether.
Avisek
 
Posts: 5
Joined: Thu Aug 15, 2013 4:12 pm

Re: Using oct2py : Calling an octave function using threads

Postby blink1073 » Wed Sep 25, 2013 7:38 am

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.
blink1073
 
Posts: 1
Joined: Wed Sep 25, 2013 7:35 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests