Jua*_*oto 11 python subprocess process xvfb pyvirtualdisplay
在使用PyVirtualDisplay时,试图找到如何避免在我们的Python应用程序中挂起Xvfb进程.基本问题是调用display.stop()(参见下面的代码示例)似乎没有正确关闭Xvfb进程.
PyVirtualDisplay非常简单:
from pyvirtualdisplay import Display
display = Display(backend='xvfb')
display.start()
... # Some stuff happens here
display.stop()
Run Code Online (Sandbox Code Playgroud)
现在,Display类稍作修改以防止Xvfb使用TCP端口:基本上,添加-nolisten tcp到执行命令.通过覆盖适当的XfvbDisplay类的_cmd属性来完成修改:
@property
def _cmd(self):
cmd = [PROGRAM,
dict(black='-br', white='-wr')[self.bgcolor],
'-screen',
str(self.screen),
'x'.join(map(str, list(self.size) + [self.color_depth])),
self.new_display_var,
'-nolisten',
'tcp'
]
return cmd
Run Code Online (Sandbox Code Playgroud)
在此上下文中结束Xvfb进程的正确方法是什么,以便它们被终止并且不会延迟?
非常感谢!
您的显示,因为它继承自EasyProcess,将具有popen属性display.popen.如果EasyProcess无法正常工作,您可以使用它来终止.
所以,你可以这样做:
display.popen.terminate()
Run Code Online (Sandbox Code Playgroud)
要么
display.popen.kill()
Run Code Online (Sandbox Code Playgroud)