From 76c14c3856a066d0fcfdd3ce46d81a0fa7f0b781 Mon Sep 17 00:00:00 2001 From: meawoppl Date: Thu, 2 Mar 2023 16:54:54 -0800 Subject: [PATCH 1/3] Try this --- scicamera/actions.py | 5 +++-- scicamera/camera.py | 34 ++++++++++------------------------ scicamera/request.py | 21 +++++++++++++++++++-- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/scicamera/actions.py b/scicamera/actions.py index 584f6958..d660b909 100644 --- a/scicamera/actions.py +++ b/scicamera/actions.py @@ -40,8 +40,9 @@ def remove_request_callback(self, callback: Callable[[CompletedRequest], None]): """ self._request_callbacks.remove(callback) - def add_completed_request(self, request: CompletedRequest) -> None: - self._requests.append(request) + def add_completed_request(self, lc_request) -> None: + wrapped_request = CompletedRequest.for_camera(self, lc_request) + self._requests.append(wrapped_request) def has_requests(self) -> bool: return len(self._requests) > 0 diff --git a/scicamera/camera.py b/scicamera/camera.py index fe87ffd7..82546add 100644 --- a/scicamera/camera.py +++ b/scicamera/camera.py @@ -79,38 +79,24 @@ def cleanup(self, index: int): def listen(self): sel = selectors.DefaultSelector() - sel.register(self.cms.event_fd, selectors.EVENT_READ, self.handle_request) + sel.register(self.cms.event_fd, selectors.EVENT_READ) while self.running: - events = sel.select(0.05) - for key, _ in events: - callback = key.data - callback() + for _ in sel.select(0.05): + self.handle_request() sel.unregister(self.cms.event_fd) - def handle_request(self, flushid=None): + def handle_request(self, flushid: int=None): """Handle requests""" with self._lock: - cams = set() for req in self.cms.get_ready_requests(): - if ( - req.status == libcamera.Request.Status.Complete - and req.cookie != flushid - ): - cams.add(req.cookie) - camera_inst = self.cameras[req.cookie] - cleanup_call = partial( - camera_inst.recycle_request, camera_inst.stop_count, req - ) - self.cameras[req.cookie].add_completed_request( - CompletedRequest( - req, - replace(camera_inst.camera_config), - camera_inst.stream_map, - cleanup_call, - ) - ) + if req.cookie == flushid: + _log.info("Flushed request %s for camera %s", req, req.cookie) + continue + + if req.status == libcamera.Request.Status.Complete: + self.cameras[req.cookie].add_completed_request(req) class Camera(RequestMachinery): diff --git a/scicamera/request.py b/scicamera/request.py index e26c1222..874458a0 100644 --- a/scicamera/request.py +++ b/scicamera/request.py @@ -6,10 +6,10 @@ import time from abc import ABC, abstractmethod from concurrent.futures import Future -from dataclasses import dataclass, field +from dataclasses import dataclass, field, replace from functools import partial from logging import getLogger -from typing import Any, Callable, Dict +from typing import Any, Callable, Dict, TYPE_CHECKING import numpy as np from PIL import Image @@ -19,6 +19,10 @@ from scicamera.configuration import CameraConfig from scicamera.lc_helpers import lc_unpack +if TYPE_CHECKING: + from scicamera.camera import Camera + + _log = getLogger(__name__) @@ -142,6 +146,19 @@ def __init__( self.cleanup = cleanup self.stream_map = stream_map + @classmethod + def for_camera(cls, camera: Camera, req): + cleanup_call = partial( + camera.recycle_request, camera.stop_count, req + ) + CompletedRequest( + req, + replace(camera.camera_config), + camera.stream_map, + cleanup_call, + ) + + def acquire(self): """Acquire a reference to this completed request, which stops it being recycled back to the camera system. From 7b99952d46cb38bb37c981d2ea22e79bdc0f1ba0 Mon Sep 17 00:00:00 2001 From: meawoppl Date: Thu, 2 Mar 2023 16:59:59 -0800 Subject: [PATCH 2/3] Small compat changes --- scicamera/actions.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/scicamera/actions.py b/scicamera/actions.py index d660b909..a89499a0 100644 --- a/scicamera/actions.py +++ b/scicamera/actions.py @@ -40,9 +40,13 @@ def remove_request_callback(self, callback: Callable[[CompletedRequest], None]): """ self._request_callbacks.remove(callback) - def add_completed_request(self, lc_request) -> None: - wrapped_request = CompletedRequest.for_camera(self, lc_request) - self._requests.append(wrapped_request) + def add_lc_request(self, lc_request) -> None: + self.add_completed_request( + CompletedRequest.for_camera(self, lc_request) + ) + + def add_completed_request(self, request: CompletedRequest) -> None: + self._requests.append(request) def has_requests(self) -> bool: return len(self._requests) > 0 From e05499372e6744fea6bd4c6efd3f64576e52e184 Mon Sep 17 00:00:00 2001 From: meawoppl Date: Thu, 2 Mar 2023 17:02:45 -0800 Subject: [PATCH 3/3] Small compat changes --- scicamera/camera.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scicamera/camera.py b/scicamera/camera.py index 82546add..5f8c3ea0 100644 --- a/scicamera/camera.py +++ b/scicamera/camera.py @@ -96,7 +96,7 @@ def handle_request(self, flushid: int=None): continue if req.status == libcamera.Request.Status.Complete: - self.cameras[req.cookie].add_completed_request(req) + self.cameras[req.cookie].add_lc_request(req) class Camera(RequestMachinery):