我有一个执行octave-cli的docker容器,我想要的是在另一个容器中将这个可执行文件作为环境变量,这样我就可以使用python脚本运行它。
我在我的python脚本中使用oct2py库,它需要一个带有octave可执行文件的环境变量。没有最新版本的octave是必需的PPA,因此我发现一个八度对接图像(mtmiller/ octave )与我的.m文件很好地工作。现在,我希望它在我的python容器中可用,以便脚本能够工作。
示例八度脚本
function hello()
printf("hello,world!")
endfunction
Python脚本
from oct2py import octave
octave.add('path/to/octavefile')
octave.hello()
输出
hello,world!
运行于python脚本之上的需求
您必须安装GNU并在PATH环境变量中安装。或者,您可以设置指向octave-cli可执行文件本身的OCTAVE_EXECUTABLE或OCTAVE环境变量。
现在,我有mtmiller/octave来执行八度。如何在python容器的路径中设置它?
发布于 2020-05-28 09:04:02
我怀疑octave提供了一个web包..。但是我不知道,但是您可以在您的octave服务器上运行一个轻量级的烧瓶api服务器。
倍频程对接图像
octave-server.py
import flask
import subprocess
from oct2py import octave
octave.add('path/to/octavefile')
app = flask.Flask(__name__)
@app.route("hello.m")
def hello_fn_call():
users_name = flask.request.args.get("name")
return json.dumps(octave.hello(users_name))
if __name__ == "__main__":
# you should really serve this with nginx + gunicorn or something
app.run(debug=True)
python对接图像
octave_rest.py
import requests
def say_hello(username):
return requests.get("http://other-image:5000/hello.m",{"name":username}).json()
if __name__ == "__main__":
print(say_hello("BOB"))
# something like "Hello, Bob"
如果您真的想要在您的八度程序中打印字符串,您将需要捕获标准输出,这个装饰应该会有所帮助(粗,您也可以手动完成)。
from contextlib import redirect_stdout # py3.4+ only
import io
...
@app.route("hello.m")
def hello_fn_call():
users_name = flask.request.args.get("name")
f = io.StringIO()
with redirect_stdout(f):
hello(users_name) # capture stdout
return json.dumps(f.getvalue())
https://stackoverflow.com/questions/62057842
复制相似问题