def start_headless_x(display=None): for _ in range(10): chosen_display = display if display else pick_random_display() headless_x = Popen(["Xvfb", chosen_display, "-ac"], stdout=PIPE, stderr=PIPE) # headless X headless_x.display = chosen_display time.sleep(0.5) # Give things time to crash if headless_x.poll() is not None: _, error = headless_x.communicate() if "Server is already active" in error: info("Server already running on %r." "Retrying on different display", chosen_display) else: raise Exception("Xvfb failed to start :%r" % error) else: info("Brought up headless X on display %r with pid %r", chosen_display, headless_x.pid) log_output(headless_x.stderr) log_output(headless_x.stdout) return headless_x else: raise Exception("Failed to find a free display" " when spawning headless X")