gpt4 book ai didi

python - oct2py - 在 python 中使用线程调用 Octave 函数

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:58 32 4
gpt4 key购买 nike

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

测试 Octave .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

最佳答案

oct2py 创建者在这里。当您从 oct2py 导入 o​​ctave 时,您正在导入 Oct2Py 类的便利实例。如果要使用多线程,则必须导入 Oct2Py 并在线程函数中实例化它,或者预先分配并将其作为参数传递给函数。 Oct2Py 的每个实例都使用自己的 Octave session 和用于 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"

关于python - oct2py - 在 python 中使用线程调用 Octave 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18228093/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com