12

Trying to find how to avoid hanging Xvfb processes in our Python application, when using PyVirtualDisplay. The essential problem is that calling display.stop() (see code sample below) does not seem to properly shut down the Xvfb process.

PyVirtualDisplay is very simply used:

from pyvirtualdisplay import Display

display = Display(backend='xvfb')
display.start()

... # Some stuff happens here

display.stop()

Now, the Display class has a slight modification to prevent Xvfb from using TCP ports: basically, add -nolisten tcp to the executing command. The modification is done by overriding the appropriate XfvbDisplay class's _cmd property:

@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

What is the proper way to end the Xvfb processes in this context so that they are terminated and do not linger?

Thanks very much!

Juan Carlos Coto
  • 11,900
  • 22
  • 62
  • 102
  • 1
    Assuming pyvirtualdisplay uses `subprocess.Popen`, you could call terminate on those objects. If you can't get access to those, then you could try using [os.kill](http://docs.python.org/2/library/os.html#os.kill) on all child processes. – alejandro Aug 29 '13 at 19:21

3 Answers3

8

Your display, since it inherits from EasyProcess, will have a popen attribute at display.popen. You can use this to terminate, if EasyProcess isn't working properly.

So, you can do something like this:

display.popen.terminate()

or

display.popen.kill()
Jordan
  • 31,971
  • 6
  • 56
  • 67
  • 1
    Thanks. What happens if I don't have access to the display object anymore? Is there a nicer way of doing a `kill` than using an OS call? – Juan Carlos Coto Sep 17 '13 at 16:59
  • 1
    That's what it's doing behind the scenes too, actually. The answer is no, not really. os.kill is pretty good actually. It's a decent interface and you can handle exceptions easily enough. – Jordan Sep 18 '13 at 03:46
8

The answer by Jordan did not work for me. This worked:

display.sendstop()
Community
  • 1
  • 1
birdnerd
  • 111
  • 2
  • 4
0

FYI, none of these solutions work for me. It says display doesn't have an attribute sendstop or popen.

ferzle
  • 85
  • 1
  • 11