rawpy icon indicating copy to clipboard operation
rawpy copied to clipboard

(cont'd) Remove gil from C implementations to enable reading raw files in parallel threads

Open jontyrudman opened this issue 2 years ago • 1 comments

Apologies for the radio silence; I do intend to get this done, I just need to work out the best way of testing that two threads have successfully run in parallel.

This work was started by kmaddock, new PR made because they were unresponsive.

jontyrudman avatar Feb 15 '24 17:02 jontyrudman

Just rebased to fix those CI issues for Python 3.12 on macos and windows.

Struggling to find a concrete way to test that the nogil stuff is working - I know it does because I've seen the benefits in my application but I can't get parallel imread and postprocess to look tangibly different from ones in series in the test func I'm working on right now.

jontyrudman avatar Feb 19 '24 21:02 jontyrudman

This worked super well for me first try, this code below now runs correctly where the main thread now correctly prints 10 times a second while the reading thread runs:

import threading
import time
from pathlib import Path

import rawpy
from PIL import Image as PIL_Im


def process_rawpy(path):
    with rawpy.imread(str(path)) as raw:
        rgb = raw.postprocess(use_camera_wb=True)

    return rgb


def open_image(path: str) -> PIL_Im:
    while True:
        if isinstance(path, str):
            path = Path(path)

        try:
            result = process_rawpy(path)
        except Exception as e:
            print(f'Failed to open raw image {path} with rawpy, falling back to Pillow: {e}')


if __name__ == "__main__":
    threading.Thread(target=open_image, args=('/path/to/arw',)).start()

    while True:
        print("Main Loop")
        time.sleep(0.1)

Would be great to get this merged, going to make my own pypi distribution for it for our own usage so having it in mainline would be great.

EmersonDove avatar May 03 '24 03:05 EmersonDove

OK, let's merge this, I think we got enough evidence that it works.

letmaik avatar May 06 '24 12:05 letmaik