Show a patch.

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

{
    "id": 16501,
    "url": "https://patchwork.libcamera.org/api/patches/16501/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/16501/",
    "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": "<20220701084521.31831-10-tomi.valkeinen@ideasonboard.com>",
    "date": "2022-07-01T08:45:13",
    "name": "[libcamera-devel,v3,09/17] py: Switch to non-blocking eventfd",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "7542f65cb2257be81370da9d64b31c49ee6f7bcd",
    "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/16501/mbox/",
    "series": [
        {
            "id": 3241,
            "url": "https://patchwork.libcamera.org/api/series/3241/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3241",
            "date": "2022-07-01T08:45:04",
            "name": "Python bindings event handling",
            "version": 3,
            "mbox": "https://patchwork.libcamera.org/series/3241/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/16501/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/16501/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 1D934BD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  1 Jul 2022 08:45:53 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 595F7656A6;\n\tFri,  1 Jul 2022 10:45:52 +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 E0B6D6565E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  1 Jul 2022 10:45:44 +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 5EC8325C;\n\tFri,  1 Jul 2022 10:45:44 +0200 (CEST)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1656665152;\n\tbh=t3z1boBAErV4KT5ni+tiw18/p6GL+055t+oTd6lKo60=;\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=OWNNLFPnVGspMGFHjZqSAlfXI2L6jscIdndblp/erm2S82IYo0FYnpITOBSSqYJ97\n\t+V+9UydUxf1AZysvxY9VyHwiyetfeWXhk/JsLAon7KprEQVtupegZXJbrXTSwVFAww\n\tinDIfN39sywjs2twuRV5NifxMZgyysaMUUF/sAon+/Rz8SKwJUhuP6smBEOVOs3eK9\n\tQFY9S48XFQj7MonqLX7tbJCulqBdjdKSNz4BHSAwV0LmZJRLqGjnaPpPgWq4QmYFxQ\n\tyKfvt+A6Dp5+dYMDAPEuTXpdxaI26byurk1pVNAZd177WzhWMkC4hTMYl71dkYy6Di\n\tLG6nwn661IPog==",
            "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1656665144;\n\tbh=t3z1boBAErV4KT5ni+tiw18/p6GL+055t+oTd6lKo60=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=LgmvwpaHqfkOnk9nD8c7/fy6NAfJKKwdoIttKomG40Bo89pTpvx8pESxVsvQhM/jz\n\tnS5KqkQW6uq+YjmqodsFHSksQ+ALtKCfUx9aNdOpFXCrNuZ+rIAvwBBsl1k7iS7IBe\n\taMrFxmoIy3VBjhBN6mFC640erznRH4DGoi/nr8xw="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"LgmvwpaH\"; 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,  1 Jul 2022 11:45:13 +0300",
        "Message-Id": "<20220701084521.31831-10-tomi.valkeinen@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.34.1",
        "In-Reply-To": "<20220701084521.31831-1-tomi.valkeinen@ideasonboard.com>",
        "References": "<20220701084521.31831-1-tomi.valkeinen@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v3 09/17] 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 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 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 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",
        "v3",
        "09/17"
    ]
}