Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completion handlers hang when running multiple instances #622

Open
torablien opened this issue Sep 8, 2024 · 0 comments
Open

Completion handlers hang when running multiple instances #622

torablien opened this issue Sep 8, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@torablien
Copy link

torablien commented Sep 8, 2024

I am using ScreenCaptureKit to capture screenshots and when I run two or more instances of the python script in parallel, the capture process silently hangs until I close all but one.

Simplified example that captures a screenshot every 2s:

import logging
import time

from AppKit import NSBitmapImageFileTypePNG, NSBitmapImageRep
from ScreenCaptureKit import (
    SCContentFilter,
    SCScreenshotManager,
    SCShareableContent,
    SCStreamConfiguration,
)


def capture_screenshot(save_path: str):
    def shareable_content_completion_handler(shareable_content, error):
        content_filter = SCContentFilter.alloc().initWithDisplay_excludingApplications_exceptingWindows_(
            shareable_content.displays()[0], [], []
        )
        configuration = SCStreamConfiguration.alloc().init()
        SCScreenshotManager.captureImageWithFilter_configuration_completionHandler_(
            content_filter, configuration, capture_image_completion_handler
        )

    def capture_image_completion_handler(image, error):
        bitmap_rep = NSBitmapImageRep.alloc().initWithCGImage_(image)
        png_data = bitmap_rep.representationUsingType_properties_(
            NSBitmapImageFileTypePNG, None
        )
        png_data.writeToFile_atomically_(save_path, True)
        logging.info(f"Screenshot saved at {save_path}")

    SCShareableContent.getShareableContentWithCompletionHandler_(
        shareable_content_completion_handler
    )

    time.sleep(1)  # use a threading.event here but it's not necessary for this example


def main():
    logging.basicConfig(level=logging.DEBUG)
    while True:
        curr_time_s = time.strftime("%Y-%m-%d_%H-%M-%S")
        capture_screenshot(save_path=f"screenshot_{curr_time_s}.png")
        time.sleep(2)

If this is the only python script doing this, it works fine. If you run another instance of it in parallel (i.e. multiple apps open), both hang silently until you close all but one. Any ideas why and/or direction to take to debug/workaround? Thanks!

Running Python 3.12 on M1 Pro 14.5

@torablien torablien added the bug Something isn't working label Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant