【问题标题】:oct2py - Calling an octave function using threads in pythonoct2py - 在 python 中使用线程调用八度函数
【发布时间】:2013-08-14 09:35:17
【问题描述】:

我试图使用两个线程从 python 程序中调用 Octave 函数。我的八度代码只是为了看看它是如何工作的-

testOctave.m

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

而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"

程序在没有任何错误的情况下退出,但在上述函数中,仅执行第一次打印,所有在 octave 调用之后的打印都被两个线程忽略。发生这种情况有什么原因吗?我该怎么做才能让它发挥作用?

该程序没有做任何特别的事情,我只是想弄清楚如何使用 oct2py

【问题讨论】:

    标签: python multithreading octave oct2py


    【解决方案1】:

    oct2py 创建者在这里。当您从 oct2py 导入 o​​ctave 时,您正在导入 Oct2Py 类的便利实例。如果要使用多个线程,则必须导入 Oct2Py 并在线程函数中实例化它,或者预先分配并将其作为参数传递给函数。每个 Oct2Py 实例都使用自己的 Octave 会话和用于 I/O 的专用 MAT 文件。

    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"
    

    【讨论】:

    • 非常感谢您的详细解答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2021-08-29
    • 1970-01-01
    相关资源
    最近更新 更多