Patch Detail
Show a patch.
GET /api/patches/17181/?format=api
{ "id": 17181, "url": "https://patchwork.libcamera.org/api/patches/17181/?format=api", "web_url": "https://patchwork.libcamera.org/patch/17181/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20220819111615.39814-7-tomi.valkeinen@ideasonboard.com>", "date": "2022-08-19T11:16:15", "name": "[libcamera-devel,v4,6/6] py: Switch to non-blocking eventfd", "commit_ref": "bf3dbaece91e7f9be00923a225a62a918bf71f5b", "pull_url": null, "state": "accepted", "archived": false, "hash": "85cf9c574f446a14d5aed586c5766c9b978ba22b", "submitter": { "id": 109, "url": "https://patchwork.libcamera.org/api/people/109/?format=api", "name": "Tomi Valkeinen", "email": "tomi.valkeinen@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/17181/mbox/", "series": [ { "id": 3428, "url": "https://patchwork.libcamera.org/api/series/3428/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3428", "date": "2022-08-19T11:16:09", "name": "Python bindings: PyCameraManager and non-blocking eventfd", "version": 4, "mbox": "https://patchwork.libcamera.org/series/3428/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/17181/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/17181/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id AF3F7C327F\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 19 Aug 2022 11:16:38 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6621661FC4;\n\tFri, 19 Aug 2022 13:16:38 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4A43A61FC9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 19 Aug 2022 13:16:33 +0200 (CEST)", "from deskari.lan (91-158-154-79.elisa-laajakaista.fi\n\t[91.158.154.79])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B8F143F1;\n\tFri, 19 Aug 2022 13:16:32 +0200 (CEST)" ], "DKIM-Signature": [ "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1660907798;\n\tbh=rbdPJmlX0jh6SOF3bqFbTjLN0VOVLWoGLtX0RjstgwE=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=IGnezcy37PpPojES2zFlMUFykQVbY4MTWzqGBj5ZUSaLvXhEr7dtTsRokIuF0fVDT\n\t9rppwr7J6VpFkwu+gNs4ZcgveFFRISlvL/jcRyVWXE/rXhJy6LjaaUVgsesyAv1NGh\n\tm6eS5vSXQFRtlI4D0bhfk7CKolx/GsqbTXANcY+SHCX/cY9W5fhHdnTOpaTA2TxGVX\n\trcxF3y7LPw0FovHSu6uuPn4N0stGx2WINDNs+5y64bttFr4d44H/BgYUzZxSeHDQBj\n\t7CuxgMxK8LVY5jTNK8yeLU3wWhJcf4mr2PtjypCks/PiDBrPy5hmdCGB8C9qqaHzaL\n\tBtle0d3bYGVIw==", "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1660907793;\n\tbh=rbdPJmlX0jh6SOF3bqFbTjLN0VOVLWoGLtX0RjstgwE=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Uod3iUNYYPOvTBRoMxIxAdSe/zpfeWbk+AJGJHTnytDxvBSvX1Ijabull7YnF4EPi\n\tnDhCxRSyQAaOkdLrEcPkFRrl4koTB0WOA/L7OSkhCOqmwe6As6N2eRFrICni8EmHum\n\tB+S3ZVodjn13ABSbuk/EUqHUAl5RAxW3Zch1qveQ=" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Uod3iUNY\"; dkim-atps=neutral", "To": "libcamera-devel@lists.libcamera.org,\n\tDavid Plowman <david.plowman@raspberrypi.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tJacopo Mondi <jacopo@jmondi.org>", "Date": "Fri, 19 Aug 2022 14:16:15 +0300", "Message-Id": "<20220819111615.39814-7-tomi.valkeinen@ideasonboard.com>", "X-Mailer": "git-send-email 2.34.1", "In-Reply-To": "<20220819111615.39814-1-tomi.valkeinen@ideasonboard.com>", "References": "<20220819111615.39814-1-tomi.valkeinen@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v4 6/6] py: Switch to non-blocking eventfd", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "From": "Tomi Valkeinen via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>", "Reply-To": "Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "Blocking wait can be easily implemented on top in Python, so rather than\nsupporting only blocking reads, or supporting both non-blocking and\nblocking reads, let's support only non-blocking reads.\n\nSigned-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/py/examples/simple-cam.py | 5 +++--\n src/py/examples/simple-capture.py | 12 +++++++++--\n src/py/examples/simple-continuous-capture.py | 5 +++--\n src/py/libcamera/py_camera_manager.cpp | 22 +++++++++++++++-----\n src/py/libcamera/py_camera_manager.h | 2 +-\n test/py/unittests.py | 7 +++++++\n 6 files changed, 41 insertions(+), 12 deletions(-)", "diff": "diff --git a/src/py/examples/simple-cam.py b/src/py/examples/simple-cam.py\nindex 2b81bb65..b3e97ca7 100755\n--- a/src/py/examples/simple-cam.py\n+++ b/src/py/examples/simple-cam.py\n@@ -19,8 +19,9 @@ TIMEOUT_SEC = 3\n \n \n def handle_camera_event(cm):\n- # cm.get_ready_requests() will not block here, as we know there is an event\n- # to read.\n+ # cm.get_ready_requests() returns the ready requests, which in our case\n+ # should almost always return a single Request, but in some cases there\n+ # could be multiple or none.\n \n reqs = cm.get_ready_requests()\n \ndiff --git a/src/py/examples/simple-capture.py b/src/py/examples/simple-capture.py\nindex a6a9b33e..5f93574f 100755\n--- a/src/py/examples/simple-capture.py\n+++ b/src/py/examples/simple-capture.py\n@@ -14,6 +14,7 @@\n \n import argparse\n import libcamera as libcam\n+import selectors\n import sys\n \n # Number of frames to capture\n@@ -107,11 +108,18 @@ def main():\n # The main loop. Wait for the queued Requests to complete, process them,\n # and re-queue them again.\n \n+ sel = selectors.DefaultSelector()\n+ sel.register(cm.event_fd, selectors.EVENT_READ)\n+\n while frames_done < TOTAL_FRAMES:\n- # cm.get_ready_requests() blocks until there is an event and returns\n- # all the ready requests. Here we should almost always get a single\n+ # cm.get_ready_requests() does not block, so we use a Selector to wait\n+ # for a camera event. Here we should almost always get a single\n # Request, but in some cases there could be multiple or none.\n \n+ events = sel.select()\n+ if not events:\n+ continue\n+\n reqs = cm.get_ready_requests()\n \n for req in reqs:\ndiff --git a/src/py/examples/simple-continuous-capture.py b/src/py/examples/simple-continuous-capture.py\nindex fe78a2dd..26a8060b 100755\n--- a/src/py/examples/simple-continuous-capture.py\n+++ b/src/py/examples/simple-continuous-capture.py\n@@ -88,8 +88,9 @@ class CaptureContext:\n camera_contexts: list[CameraCaptureContext] = []\n \n def handle_camera_event(self):\n- # cm.get_ready_requests() will not block here, as we know there is an event\n- # to read.\n+ # cm.get_ready_requests() returns the ready requests, which in our case\n+ # should almost always return a single Request, but in some cases there\n+ # could be multiple or none.\n \n reqs = self.cm.get_ready_requests()\n \ndiff --git a/src/py/libcamera/py_camera_manager.cpp b/src/py/libcamera/py_camera_manager.cpp\nindex ea4ae825..9ccb7aad 100644\n--- a/src/py/libcamera/py_camera_manager.cpp\n+++ b/src/py/libcamera/py_camera_manager.cpp\n@@ -24,7 +24,7 @@ PyCameraManager::PyCameraManager()\n \n \tcameraManager_ = std::make_unique<CameraManager>();\n \n-\tint fd = eventfd(0, EFD_CLOEXEC);\n+\tint fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);\n \tif (fd == -1)\n \t\tthrow std::system_error(errno, std::generic_category(),\n \t\t\t\t\t\"Failed to create eventfd\");\n@@ -62,7 +62,13 @@ py::list PyCameraManager::cameras()\n \n std::vector<py::object> PyCameraManager::getReadyRequests()\n {\n-\treadFd();\n+\tint ret = readFd();\n+\n+\tif (ret == -EAGAIN)\n+\t\treturn std::vector<py::object>();\n+\n+\tif (ret != 0)\n+\t\tthrow std::system_error(-ret, std::generic_category());\n \n \tstd::vector<py::object> py_reqs;\n \n@@ -96,12 +102,18 @@ void PyCameraManager::writeFd()\n \t\tLOG(Python, Fatal) << \"Unable to write to eventfd\";\n }\n \n-void PyCameraManager::readFd()\n+int PyCameraManager::readFd()\n {\n \tuint8_t buf[8];\n \n-\tif (read(eventFd_.get(), buf, 8) != 8)\n-\t\tthrow std::system_error(errno, std::generic_category());\n+\tssize_t ret = read(eventFd_.get(), buf, 8);\n+\n+\tif (ret == 8)\n+\t\treturn 0;\n+\telse if (ret < 0)\n+\t\treturn -errno;\n+\telse\n+\t\treturn -EIO;\n }\n \n void PyCameraManager::pushRequest(Request *req)\ndiff --git a/src/py/libcamera/py_camera_manager.h b/src/py/libcamera/py_camera_manager.h\nindex 56bea13d..3525057d 100644\n--- a/src/py/libcamera/py_camera_manager.h\n+++ b/src/py/libcamera/py_camera_manager.h\n@@ -39,7 +39,7 @@ private:\n \t\tLIBCAMERA_TSA_GUARDED_BY(completedRequestsMutex_);\n \n \tvoid writeFd();\n-\tvoid readFd();\n+\tint readFd();\n \tvoid pushRequest(Request *req);\n \tstd::vector<Request *> getCompletedRequests();\n };\ndiff --git a/test/py/unittests.py b/test/py/unittests.py\nindex 9adc4337..6dea80fc 100755\n--- a/test/py/unittests.py\n+++ b/test/py/unittests.py\n@@ -207,9 +207,16 @@ class SimpleCaptureMethods(CameraTesterBase):\n reqs = None\n gc.collect()\n \n+ sel = selectors.DefaultSelector()\n+ sel.register(cm.event_fd, selectors.EVENT_READ)\n+\n reqs = []\n \n while True:\n+ events = sel.select()\n+ if not events:\n+ continue\n+\n ready_reqs = cm.get_ready_requests()\n \n reqs += ready_reqs\n", "prefixes": [ "libcamera-devel", "v4", "6/6" ] }