简体   繁体   English

oct2py - 使用python中的线程调用八度函数

[英]oct2py - Calling an octave function using threads in python

I was trying to call an Octave function from a python program using two threads. 我试图使用两个线程从python程序调用Octave函数。 My octave code is just to see how it works - 我的八度代码只是为了看它是如何工作的 -

testOctave.m testOctave.m

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

And the python program just tries to call it 而python程序只是试图调用它

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 该程序没有做任何特别的事情,我只想弄清楚如何使用oct2py

oct2py creator here. oct2py创建者在这里。 When you import octave from oct2py you are importing a convenience instance of the Oct2Py class. 从oct2py导入八度音程时,您正在导入Oct2Py类的便捷实例。 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. 如果要使用多个线程,则必须导入Oct2Py并在线程函数内实例化或预分配,并将其作为参数传递给函数。 Each instance of Oct2Py uses its own Octave session and dedicated MAT files for I/O. Oct2Py的每个实例都使用自己的Octave会话和专用的MAT文件进行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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM