Subprocess 语句在 python 控制台中有效,但在服务器密度插件中不起作用?
当我将
$ python
>>> import subprocess
>>> r = subprocess.Popen(['/pathto/plugin1.rb'], stdout=subprocess.PIPE, close_fds=True).communicate()[0]
>>> data = eval(r)
>>> data
{'test': 1}
其转换为 服务器密度插件但是每次执行插件时它都会使agent.py守护进程崩溃。我能够将其范围缩小到子流程线,但无法找出原因。异常捕获似乎也不起作用。
这个插件是这样的:
class plugin1:
def run(self):
r = subprocess.Popen(['/pathto/plugin1.rb'], stdout=subprocess.PIPE, close_fds=True).communicate()[0]
data = eval(r)
return data
我对使用 python 还很陌生,无法真正弄清楚为什么这不起作用。非常感谢您的想法:)
in the python console the following statement works perfectly fine (i guess using eval that way is not really good, but its just for testing purpose in this case and will be replaced with proper parsing)
$ python
>>> import subprocess
>>> r = subprocess.Popen(['/pathto/plugin1.rb'], stdout=subprocess.PIPE, close_fds=True).communicate()[0]
>>> data = eval(r)
>>> data
{'test': 1}
when i convert this into a Serverdensity plugin however it keeps crashing the agent.py daemon everytime it executes the plugin. i was able to narrow it down to the subprocess line but could not find out why. exception catching did not seem to work also.
here how the plugin looks like:
class plugin1:
def run(self):
r = subprocess.Popen(['/pathto/plugin1.rb'], stdout=subprocess.PIPE, close_fds=True).communicate()[0]
data = eval(r)
return data
I'm quite new to work with python and cant really figure out why this wont work. Thanks a lot for ideas :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在模块中导入了子流程吗?另外,您遇到了什么错误,您可以发布错误消息吗?
Do you have subprocess imported in the module? Also what error are you getting could you post the error message ?
切换我的开发盒后(也许是因为 python 版本不同?)我终于能够得到一些正确的错误输出。
那么事情就相当简单了:我真的只需要导入缺少的子流程模块。
对于对该解决方案感兴趣的人:
http://github。 com/maxigs/Server密度-Wrapper-Plugin/blob/master/ruby_plugin.py
尚未完全准备好生产,但已可用于保存输入
After switching my dev box (maybe because of the different python version?) i finally was able to get some proper error output.
Then it was rather simple: I really just needed to import the missing subprocess module.
For who is interested in the solution:
http://github.com/maxigs/Serverdensity-Wrapper-Plugin/blob/master/ruby_plugin.py
Not quite production ready yet, but works already for save input