GeezerVison is showing github.com — [tutorial] add T07-MultiPipeline tutorial for python · feiyunwill/CGraph@51ba7fc
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 | /python/dist | 19 | 19 | /python/PyCGraph.egg-info | 20 | 20 | __pycache__ | | 21 | +*.pyi |
Collapse file Copy file name to clipboardExpand all lines: python/PyCGraph.cpp +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,17 @@ PYBIND11_MODULE(PyCGraph, m) { | 38 | 38 | .def_readwrite("batch_task_enable", &UThreadPoolConfig::batch_task_enable_) | 39 | 39 | .def_readwrite("monitor_enable", &UThreadPoolConfig::monitor_enable_); | 40 | 40 |
| | 41 | + py::class_<UThreadPool>(m, "UThreadPool") | | 42 | + .def(py::init<CBool, const UThreadPoolConfig&>(), | | 43 | + py::arg("autoInit") = true, | | 44 | + py::arg("config") = UThreadPoolConfig{}) | | 45 | + .def("setConfig", &UThreadPool::setConfig, | | 46 | + py::keep_alive<1, 2>()) | | 47 | + .def("getConfig", &UThreadPool::getConfig) | | 48 | + .def("init", &UThreadPool::init) | | 49 | + .def("destroy", &UThreadPool::destroy) | | 50 | + .def("isInit", &UThreadPool::isInit); | | 51 | + | 41 | 52 | py::enum_<GEngineType>(m, "GEngineType") | 42 | 53 | .value("DYNAMIC", GEngineType::DYNAMIC) | 43 | 54 | .value("TOPO", GEngineType::TOPO) | @@ -151,6 +162,9 @@ PYBIND11_MODULE(PyCGraph, m) { | 151 | 162 | .def("hasGParam", &PyGPipeline::__hasGParam_4py, | 152 | 163 | py::call_guard<py::gil_scoped_release>()) | 153 | 164 | .def("setUniqueThreadPoolConfig", &PyGPipeline::setUniqueThreadPoolConfig) | | 165 | + .def("setSharedThreadPool", &PyGPipeline::setSharedThreadPool, | | 166 | + py::call_guard<py::gil_scoped_release>(), | | 167 | + py::keep_alive<1, 2>()) | 154 | 168 | .def("setGEngineType", &PyGPipeline::setGEngineType) | 155 | 169 | .def("run", &PyGPipeline::run, | 156 | 170 | py::call_guard<py::gil_scoped_release>()) | |
Collapse file Copy file name to clipboard +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +""" | | 2 | +@Author: Chunel | | 3 | + | | 4 | +@File: T07-MultiPipeline | | 5 | +@Time: 2025/4/1 23:37 | | 6 | +@Desc: | | 7 | +""" | | 8 | + | | 9 | +from PyCGraph import GPipeline, UThreadPoolConfig, UThreadPool | | 10 | + | | 11 | +from MyGNode.MyNode1 import MyNode1 | | 12 | +from MyGNode.MyNode2 import MyNode2 | | 13 | + | | 14 | + | | 15 | +def async_pipeline_1(pipeline: GPipeline): | | 16 | + a, b, c, d = MyNode1(), MyNode2(), MyNode1(), MyNode2() | | 17 | + | | 18 | + pipeline.registerGElement(a, set(), "node1A") | | 19 | + pipeline.registerGElement(b, {a}, "node1B") | | 20 | + pipeline.registerGElement(c, {a}, "node1C") | | 21 | + pipeline.registerGElement(d, {a}, "node1D") | | 22 | + | | 23 | + return pipeline.asyncProcess(3) | | 24 | + | | 25 | + | | 26 | +def async_pipeline_2(pipeline: GPipeline): | | 27 | + a, b, c = MyNode1(), MyNode2(), MyNode1() | | 28 | + | | 29 | + pipeline.registerGElement(a, set(), "node2A") | | 30 | + pipeline.registerGElement(b, {a}, "node2B") | | 31 | + pipeline.registerGElement(c, {b}, "node2C") | | 32 | + | | 33 | + return pipeline.asyncProcess(5) | | 34 | + | | 35 | + | | 36 | +def tutorial_multi_pipeline(): | | 37 | + pipeline1, pipeline2 = GPipeline(), GPipeline() | | 38 | + | | 39 | + config = UThreadPoolConfig() | | 40 | + config.default_thread_size = 4 | | 41 | + config.max_thread_size = 4 | | 42 | + config.monitor_enable = False | | 43 | + pool = UThreadPool(True, config) | | 44 | + | | 45 | + pipeline1.setSharedThreadPool(pool) | | 46 | + pipeline2.setSharedThreadPool(pool) | | 47 | + | | 48 | + result1 = async_pipeline_1(pipeline1) | | 49 | + result2 = async_pipeline_2(pipeline2) | | 50 | + | | 51 | + result1.wait() | | 52 | + result2.wait() | | 53 | + | | 54 | + | | 55 | +if __name__ == '__main__': | | 56 | + tutorial_multi_pipeline() |
|