{"id":16343,"url":"https://patchwork.libcamera.org/api/patches/16343/?format=json","web_url":"https://patchwork.libcamera.org/patch/16343/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220623144736.78537-6-tomi.valkeinen@ideasonboard.com>","date":"2022-06-23T14:47:34","name":"[libcamera-devel,RFC,v1,5/7] py: Add 'nonblocking' argument to get_ready_requests()","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"d4a4febbf5342dc90f7b0ea6729b9eb17e767847","submitter":{"id":109,"url":"https://patchwork.libcamera.org/api/people/109/?format=json","name":"Tomi Valkeinen","email":"tomi.valkeinen@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16343/mbox/","series":[{"id":3213,"url":"https://patchwork.libcamera.org/api/series/3213/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3213","date":"2022-06-23T14:47:29","name":"Python bindings event handling","version":1,"mbox":"https://patchwork.libcamera.org/series/3213/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16343/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16343/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 CC360BD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 23 Jun 2022 14:48:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 277F165647;\n\tThu, 23 Jun 2022 16:48:06 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F1A6B65636\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Jun 2022 16:47:59 +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 7069CDD;\n\tThu, 23 Jun 2022 16:47:59 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1655995686;\n\tbh=ypyZr0h0B0esoBDRGJv43rUnwKdehaP63OxAJslBc+Q=;\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=syCXpx4nCTb3CpWpur5eimpbM1ouvl8Wf7ZGoMW66KD88prcwYC+Quh7z7qAhtWtF\n\tYzY5azWSrzuBcO9ZQq471jS2s40JVw5qXIBt/zv1F1+MqUF1fiIm+0n3chHlz8eeWC\n\teGb6UMsJ8Rpa3xK9gOeg0avcnY/uu8lRfcyhD/kaKiHBeFQSEULDIzk3op4GIRSZly\n\tx0fV/czK/PsWUdRHSD8paAnjN5hw9T3vBbBbTrOgHlQ77L0hfrMJev0jp3SoPnK3+r\n\tZMKny3lO1EgcoHSTfiP6UY1v4JgjID76ynDn3np0F0sstDugUjJhILlIu59tZLrFj7\n\t+WWKilP5c6ECQ==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1655995679;\n\tbh=ypyZr0h0B0esoBDRGJv43rUnwKdehaP63OxAJslBc+Q=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=BpJnU9oRv4qBXgVVXu9gwToebJ2SukHbpw1jufxqaOvqM09Onmns2hwv4GwWAqaGA\n\tUpgW2jmKMXZ86IyvHvMnVdLjj1G4kIx41SlwvfQr3J+34q7bopOjxUsHyPTHgsaDPP\n\tO6FSOQeUUbzzslLQgtowoV29IxdNSK3nDyRMwUXI="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"BpJnU9oR\"; 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":"Thu, 23 Jun 2022 17:47:34 +0300","Message-Id":"<20220623144736.78537-6-tomi.valkeinen@ideasonboard.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20220623144736.78537-1-tomi.valkeinen@ideasonboard.com>","References":"<20220623144736.78537-1-tomi.valkeinen@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC v1 5/7] py: Add 'nonblocking' argument to\n\tget_ready_requests()","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":"Add 'nonblocking' argument to get_ready_requests() which allows the user\nto ensure get_ready_requests() never blocks. This can be used e.g. after\ncalling camera.stop(), to process or discard any ready or cancelled\nRequests.\n\nIn fact, it probably should always be used after stopping the cameras,\nunless you have made sure that there are no unprocessed Requests. If you\nstart the camera again, and you have left Requests unprocessed, you will\nget those \"old\" Requests when you expect to get the new Requests.\n\nIt may be good to call this even if your script exits after stopping\nthe cameras, as unprocessed Requests will keep the Cameras and related\nobjects alive, and thus they won't be freed. As your script is exiting\nit's strictly speaking not an issue, but it does make tracking other\n\"real\" memory leaks more difficult.\n\nPerhaps the camera.start() should go and discard any old Requests\nrelated to that camera. For the exit issue I don't see any automatic\nsolution.\n\nSigned-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>\n---\n src/py/libcamera/py_camera_manager.cpp | 21 +++++++++++++++++++--\n src/py/libcamera/py_camera_manager.h   |  4 +++-\n src/py/libcamera/py_main.cpp           |  3 ++-\n 3 files changed, 24 insertions(+), 4 deletions(-)","diff":"diff --git a/src/py/libcamera/py_camera_manager.cpp b/src/py/libcamera/py_camera_manager.cpp\nindex c9e5a99c..ba45f713 100644\n--- a/src/py/libcamera/py_camera_manager.cpp\n+++ b/src/py/libcamera/py_camera_manager.cpp\n@@ -5,6 +5,7 @@\n \n #include \"py_camera_manager.h\"\n \n+#include <poll.h>\n #include <sys/eventfd.h>\n #include <unistd.h>\n \n@@ -55,9 +56,10 @@ py::list PyCameraManager::getCameras()\n \treturn l;\n }\n \n-std::vector<py::object> PyCameraManager::getReadyRequests()\n+std::vector<py::object> PyCameraManager::getReadyRequests(bool nonBlocking)\n {\n-\treadFd();\n+\tif (!nonBlocking || hasEvents())\n+\t\treadFd();\n \n \tstd::vector<Request *> v;\n \tgetRequests(v);\n@@ -113,3 +115,18 @@ void PyCameraManager::getRequests(std::vector<Request *> &v)\n \tstd::lock_guard guard(reqlist_mutex_);\n \tswap(v, reqList_);\n }\n+\n+bool PyCameraManager::hasEvents()\n+{\n+\tstruct pollfd pfd = {\n+\t\t.fd = eventFd_,\n+\t\t.events = POLLIN,\n+\t\t.revents = 0,\n+\t};\n+\n+\tint ret = poll(&pfd, 1, 0);\n+\tif (ret == -1)\n+\t\tthrow std::system_error(errno, std::generic_category());\n+\n+\treturn pfd.revents & POLLIN;\n+}\ndiff --git a/src/py/libcamera/py_camera_manager.h b/src/py/libcamera/py_camera_manager.h\nindex b0b971ad..2396d236 100644\n--- a/src/py/libcamera/py_camera_manager.h\n+++ b/src/py/libcamera/py_camera_manager.h\n@@ -23,7 +23,7 @@ public:\n \n \tint eventFd() const { return eventFd_; }\n \n-\tstd::vector<pybind11::object> getReadyRequests();\n+\tstd::vector<pybind11::object> getReadyRequests(bool nonBlocking = false);\n \n \tvoid handleRequestCompleted(Request *req);\n \n@@ -36,4 +36,6 @@ private:\n \tvoid readFd();\n \tvoid pushRequest(Request *req);\n \tvoid getRequests(std::vector<Request *> &v);\n+\n+\tbool hasEvents();\n };\ndiff --git a/src/py/libcamera/py_main.cpp b/src/py/libcamera/py_main.cpp\nindex 23018288..ee4ecb9b 100644\n--- a/src/py/libcamera/py_main.cpp\n+++ b/src/py/libcamera/py_main.cpp\n@@ -103,7 +103,8 @@ PYBIND11_MODULE(_libcamera, m)\n \t\t.def_property_readonly(\"cameras\", &PyCameraManager::getCameras)\n \n \t\t.def_property_readonly(\"event_fd\", &PyCameraManager::eventFd)\n-\t\t.def(\"get_ready_requests\", &PyCameraManager::getReadyRequests);\n+\t\t.def(\"get_ready_requests\", &PyCameraManager::getReadyRequests,\n+\t\t     py::arg(\"nonblocking\") = false);\n \n \tpyCamera\n \t\t.def_property_readonly(\"id\", &Camera::id)\n","prefixes":["libcamera-devel","RFC","v1","5/7"]}