如何在功能较弱的服务器上通过 php 调用 python scipt 以在其他功能更强大的服务器(集群)上运行?
我有一个 action.php 脚本,我从其中调用一个 python 脚本,如下所示:
$call_python = "../python/python myPythonScript.py ".$someArgument;
$python_output = Null; // stores every output generated by myPythonScript.py
$mystring = exec($call_python, $output_python);
我知道,这可能不是最好的方法,但到目前为止它对我有用。最近我发现,实际服务器的计算能力不够。所以我希望 python 脚本在同一网络中更强大的服务器(集群)上运行。
这可能吗?
I have a action.php script from where I call a python script like this:
$call_python = "../python/python myPythonScript.py ".$someArgument;
$python_output = Null; // stores every output generated by myPythonScript.py
$mystring = exec($call_python, $output_python);
I know, this is perhaps not the best way, but it works for me so far. Recently I discovered, that the computational power of the actual server is not sufficient. So I want the python script to be run on a more powerful server (a cluster) which is in the same network.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以轻松地将
exec()
调用替换为ssh2_exec()
。但是,要获取输出,请使用stream_get_context()
。这可能是这里最明智的解决方案。您只需事先打开与辅助服务器的连接即可。请参阅
ssh2_connect
和ssh2_auth_password
为此。当然,您需要安装并启用 ssh2 PHP 扩展。You can easily replace your
exec()
call withssh2_exec()
. To get the output usestream_get_context()
however. That's probably the sanest solution here.You just need to open a connection to your secondary server beforehand. See
ssh2_connect
andssh2_auth_password
for that. And of course you need the ssh2 PHP extension installed and enabled.