Show a patch.

GET /api/1.1/patches/16416/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 16416,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/16416/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/16416/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/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": "<20220629070416.17550-11-tomi.valkeinen@ideasonboard.com>",
    "date": "2022-06-29T07:04:12",
    "name": "[libcamera-devel,v2,10/14] py: Switch to non-blocking eventfd",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "7542f65cb2257be81370da9d64b31c49ee6f7bcd",
    "submitter": {
        "id": 109,
        "url": "https://patchwork.libcamera.org/api/1.1/people/109/?format=api",
        "name": "Tomi Valkeinen",
        "email": "tomi.valkeinen@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/16416/mbox/",
    "series": [
        {
            "id": 3230,
            "url": "https://patchwork.libcamera.org/api/1.1/series/3230/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3230",
            "date": "2022-06-29T07:04:02",
            "name": "Python bindings event handling",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/3230/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/16416/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/16416/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 E72F8BE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 29 Jun 2022 07:04:57 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9867D60552;\n\tWed, 29 Jun 2022 09:04:57 +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 1B29D60552\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 29 Jun 2022 09:04:46 +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 790A84A8;\n\tWed, 29 Jun 2022 09:04:45 +0200 (CEST)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1656486297;\n\tbh=/Yv54VY4nsytbaSDp75PRYRZjCs0hmsPbp/8nIPGblY=;\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=NXXiXZEp6YHjYyUPJ3cAhw25rcZYegrnpfGTur/RU0X42iGScv5vp5EBI1dlULRsn\n\t9SxMcLHqnQBZvjz0+/7LhGtfH/1MoQqCOmQH9E7SbVyN83lQ8c04lj5p00V5rS2/9P\n\tH2NFMw2fbwMS4hbqzq8QlTZQLAf0UixbkoHGgWsKe/qhCLG4A/Dp21fPQOkyrZ3A+r\n\tupirUUQO17Ha53mMjUtrsmRwKGi4ksrZnW0ZLv+zcy1zWuu+X/I06bIp/agtWE4/un\n\tTQhbqOkf+QSrx3KyxdosQFfj8xDVonZctJHpWmzUwnhSS2XH+yG0txW7/UZDCs7mH7\n\tmQF80LvtlL9dQ==",
            "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1656486285;\n\tbh=/Yv54VY4nsytbaSDp75PRYRZjCs0hmsPbp/8nIPGblY=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=J/CEFeN0OjR5rsJ5XTLiDpktls9pIWxJXWXHXZrpX6IJFNqnPNC5ZjSmE1qbGOOun\n\tpLmQ/fpcU4KIML47/k+tL+bUVaZQWoYU94Xt6+3TCIE/aPIf6SKoC2bZsryyn+l9U7\n\thx+W05/eeY/Pb/gsgJ0GaTEwfiD6D5xmrqchosZo="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"J/CEFeN0\"; 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": "Wed, 29 Jun 2022 10:04:12 +0300",
        "Message-Id": "<20220629070416.17550-11-tomi.valkeinen@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.34.1",
        "In-Reply-To": "<20220629070416.17550-1-tomi.valkeinen@ideasonboard.com>",
        "References": "<20220629070416.17550-1-tomi.valkeinen@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v2 10/14] py: Switch to non-blocking\n\teventfd",
        "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>\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, 39 insertions(+), 14 deletions(-)",
    "diff": "diff --git a/src/py/examples/simple-cam.py b/src/py/examples/simple-cam.py\nindex e88a4737..1cd1019d 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 07d12dae..4b85408f 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@@ -102,11 +103,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 ef3f87d1..e1cb931e 100755\n--- a/src/py/examples/simple-continuous-capture.py\n+++ b/src/py/examples/simple-continuous-capture.py\n@@ -83,8 +83,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 5600f661..3dd8668e 100644\n--- a/src/py/libcamera/py_camera_manager.cpp\n+++ b/src/py/libcamera/py_camera_manager.cpp\n@@ -22,7 +22,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@@ -60,18 +60,24 @@ py::list PyCameraManager::cameras()\n \n std::vector<py::object> PyCameraManager::getReadyRequests()\n {\n-\treadFd();\n+\tint ret = readFd();\n \n-\tstd::vector<py::object> ret;\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 \tfor (Request *request : getCompletedRequests()) {\n \t\tpy::object o = py::cast(request);\n \t\t/* Decrease the ref increased in Camera.queue_request() */\n \t\to.dec_ref();\n-\t\tret.push_back(o);\n+\t\tpy_reqs.push_back(o);\n \t}\n \n-\treturn ret;\n+\treturn py_reqs;\n }\n \n /* Note: Called from another thread */\n@@ -94,12 +100,14 @@ 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+\t\treturn errno;\n+\n+\treturn 0;\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 b90b5fec..794e46be 100755\n--- a/test/py/unittests.py\n+++ b/test/py/unittests.py\n@@ -191,9 +191,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",
        "v2",
        "10/14"
    ]
}