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

Small Rework on CameraManager for clarity #72

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scicamera/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def remove_request_callback(self, callback: Callable[[CompletedRequest], None]):
"""
self._request_callbacks.remove(callback)

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)

Expand Down
17 changes: 9 additions & 8 deletions scicamera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ 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)

Expand All @@ -101,14 +99,17 @@ def handle_request(self, flushid: int | None = None) -> int:
n_flushed = 0
with self._lock:
for req in self.cms.get_ready_requests():
if req.status != libcamera.Request.Status.Complete:
_log.warning("Unexpected request status: %s", req.status)
continue
if req.cookie == flushid:
_log.warning("Flushing request.")
n_flushed += 1
continue

if req.status == libcamera.Request.Status.Complete:
self.cameras[req.cookie].add_lc_request(req)
else:
_log.warning("Unexpected request status: %s", req.status)
continue

camera_inst = self.cameras[req.cookie]
cleanup_call = partial(
camera_inst.recycle_request, camera_inst.stop_count, req
Expand Down
21 changes: 19 additions & 2 deletions scicamera/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +21,10 @@
from scicamera.lc_helpers import lc_unpack
from scicamera.sensor_format import SensorFormat

if TYPE_CHECKING:
from scicamera.camera import Camera


_log = getLogger(__name__)


Expand Down Expand Up @@ -145,6 +149,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.
Expand Down