ponty / pyscreenshot

Python screenshot library, replacement for the Pillow ImageGrab module on Linux.
BSD 2-Clause "Simplified" License
499 stars 89 forks source link

If subprocess is disabled, what sort of issues can happen? #79

Closed anwar6953 closed 3 years ago

anwar6953 commented 4 years ago

From the README: "Disabling this option makes performance much better, but it may cause problems in some cases."

What sort of issues have you run into when calling mss in the same process? In-process performance is substantially better, but I'm curious to know what sort of issues can arise.

ponty commented 4 years ago

There are different issues with different backends. MSS doesn't support multiple X displays, so this will crash:

import mss
from pyvirtualdisplay import Display

with Display():
    sct = mss.mss()
    img = sct.grab((10, 20, 30, 40))
    print(img)
with Display():
    sct = mss.mss()
    img = sct.grab((10, 20, 30, 40))
    print(img)

Test is in double_disp.py

mss code:

    def _get_display(self, disp=None):
        """
        Retrieve a thread-safe display from XOpenDisplay().
        In multithreading, if the thread who creates *display* is dead, *display* will
        no longer be valid to grab the screen. The *display* attribute is replaced
        with *_display_dict* to maintain the *display* values in multithreading.
        Since the current thread and main thread are always alive, reuse their
        *display* value first.
        """
        cur_thread, main_thread = threading.current_thread(), threading.main_thread()
        display = MSS._display_dict.get(cur_thread) or MSS._display_dict.get(
            main_thread
        )
        if not display:
            display = MSS._display_dict[cur_thread] = self.xlib.XOpenDisplay(disp)
        return display

The problem is that it reuses the display.

anwar6953 commented 4 years ago

Got it. I'll be sure to keep that in mind as I use multiple displays. For my use case, I'll try to limit it to a single display and use mss, because it is so fast. Thanks!