简体   繁体   English

包含pyCuda时,shell_exec的输出不再返回到PHP

[英]Output from shell_exec is no longer returned to PHP when including pyCuda

I am calling this python file: 我正在调用此python文件:

import sys
if __name__ == "__main__":
    print sys.argv[1]

From PHP like this: 从PHP像这样:

$param = "hello";
$result = shell_exec("python /path/to/python/file.py " . $param);
echo $result;

Which works great as hello is displayed when running the PHP file. 运行PHP文件时,当显示hello时效果很好。 However, when I import pyCuda (2012.1) into the python file, the output is no longer returned to PHP even though it is still shown when running the command from bash. 但是,当我将pyCuda(2012.1)导入python文件时,即使从bash运行命令时仍然显示输出,输出也不再返回到PHP。 This is the python file with pyCuda imported: 这是导入了pyCuda的python文件:

from pycuda import driver as drv
from pycuda import tools
from pycuda import autoinit
import sys
if __name__ == "__main__":
    print sys.argv[1]

Why does PHP no longer receive the output when pyCuda is imported and what is the best way to read the output from this python file in PHP? 为什么导入pyCuda时PHP不再接收输出,最好的方法是从PHP中的此python文件读取输出?

Many thanks! 非常感谢!

pycuda.autoinit registers an atexit autohandler to clean up the CUDA context it creates so that the python code exits gracefully. pycuda.autoinit注册一个atexit自动处理程序,以清理它创建的CUDA上下文,以便python代码正常退出。 It is likely that this is terminating the application without flushing stdout, so the output never gets emitted. 这很可能在不刷新标准输出的情况下终止了应用程序,因此永远不会发出输出。

So either try explicit flushing of stdout: 因此,要么尝试显式刷新标准输出:

import sys
print sys.argv[1]
sys.stdout.flush()

or don't use pycuda.autoinit and handle setup and pull down of the driver API context yourself. 或不要使用pycuda.autoinit处理设置和下拉驱动程序API上下文。

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

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