GeezerVison is showing github.com — [chg] change yield to suspend · feiyunwill/CGraph@8e35360
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: python/PyCGraph.cpp +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,6 +91,14 @@ PYBIND11_MODULE(PyCGraph, m) { | 91 | 91 | .value("DESTROY", CFunctionType::DESTROY) | 92 | 92 | .export_values(); | 93 | 93 |
| | 94 | + py::enum_<GElementState>(m, "GElementState") | | 95 | + .value("NORMAL", GElementState::NORMAL) | | 96 | + .value("CANCEL", GElementState::CANCEL) | | 97 | + .value("SUSPEND", GElementState::SUSPEND) | | 98 | + .value("TIMEOUT", GElementState::TIMEOUT) | | 99 | + .export_values(); | | 100 | + m.attr("GPipelineState") = m.attr("GElementState"); | | 101 | + | 94 | 102 | py::enum_<std::launch>(m, "StdLaunchPolicy") | 95 | 103 | .value("ASYNC", std::launch::async) | 96 | 104 | .value("DEFERRED", std::launch::deferred) | @@ -190,7 +198,7 @@ PYBIND11_MODULE(PyCGraph, m) { | 190 | 198 | py::call_guard<py::gil_scoped_release>()) | 191 | 199 | .def("cancel", &PywGPipeline::cancel, | 192 | 200 | py::call_guard<py::gil_scoped_release>()) | 193 | | - .def("yields", &PywGPipeline::yield, // yield is python's keyword, use yields to replace | | 201 | + .def("suspend", &PywGPipeline::suspend, | 194 | 202 | py::call_guard<py::gil_scoped_release>()) | 195 | 203 | .def("resume", &PywGPipeline::resume, | 196 | 204 | py::call_guard<py::gil_scoped_release>()) | |
Collapse file Copy file name to clipboardExpand all lines: python/tutorial/MyGNode/MyStageNode.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,7 +21,7 @@ def run(self): | 21 | 21 | time.sleep(self._before) | 22 | 22 | print('[{0}] ----> {1} wait {2}s before enter stage'.format(datetime.now(), self.getName(), self._before)) | 23 | 23 |
| 24 | | - sts: CStatus = self.enterStage('stage1') | | 24 | + status: CStatus = self.enterStage('stage1') | 25 | 25 | print('[{0}] <---- {1} wait {2}s after enter stage'.format(datetime.now(), self.getName(), self._after)) | 26 | 26 | time.sleep(self._after) | 27 | | - return CStatus() | | 27 | + return status |
Collapse file Copy file name to clipboardExpand all lines: python/tutorial/T12-Function.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,7 +25,7 @@ def d_function_init(d: GFunction): | 25 | 25 | return CStatus() | 26 | 26 |
| 27 | 27 | def d_function_run(d: GFunction, num: int): | 28 | | - # get GParam info in CFunction | | 28 | + # get GParam info in GFunction | 29 | 29 | param: MyParam = d.getGParamWithNoEmpty('param1') | 30 | 30 | param.count += num | 31 | 31 | print('[{0}] do run function, count is {1}, value is {2}'.format( | |
Collapse file Copy file name to clipboardExpand all lines: python/tutorial/T13-Daemon.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | """ | 2 | 2 | @Author: Chunel | 3 | 3 | | 4 | | -@File: T13-Deamon | | 4 | +@File: T13-Daemon | 5 | 5 | @Time: 2025/3/30 00:46 | 6 | 6 | @Desc: | 7 | 7 | """ | | 13 | 13 | from MyGDaemon.MyMonitorDaemon import MyMonitorGDaemon | 14 | 14 |
| 15 | 15 |
| 16 | | -def tutorial_simple(): | | 16 | +def tutorial_daemon(): | 17 | 17 | pipeline = GPipeline() | 18 | 18 | a, b = MyNode1(), MyWriteParamNode() | 19 | 19 |
| @@ -26,5 +26,5 @@ def tutorial_simple(): | 26 | 26 |
| 27 | 27 |
| 28 | 28 | if __name__ == '__main__': | 29 | | - tutorial_simple() | | 29 | + tutorial_daemon() | 30 | 30 |
|
Collapse file Copy file name to clipboardExpand all lines: python/tutorial/T20-Suspend.py +5-6Lines changed: 5 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | """ | 2 | 2 | @Author: Chunel | 3 | 3 | | 4 | | -@File: T20-YieldResume | | 4 | +@File: T20-Suspend | 5 | 5 | @Time: 2025/3/29 00:23 | 6 | 6 | @Desc: | 7 | 7 | """ | 8 | 8 |
| 9 | | - | 10 | 9 | import time | 11 | 10 |
| 12 | 11 | from PyCGraph import GPipeline, StdFutureCStatus | | 15 | 14 | from MyGNode.MyNode2 import MyNode2 | 16 | 15 |
| 17 | 16 |
| 18 | | -def tutorial_cancel(): | | 17 | +def tutorial_suspend(): | 19 | 18 | pipeline = GPipeline() | 20 | 19 | a, b, c, d = MyNode1(), MyNode2(), MyNode1(), MyNode2() | 21 | 20 |
| @@ -29,8 +28,8 @@ def tutorial_cancel(): | 29 | 28 | result: StdFutureCStatus = pipeline.asyncRun() | 30 | 29 | print("pipeline async run first time, BEGIN.") | 31 | 30 | time.sleep(2.6) | 32 | | - print("pipeline async run first time, YIELDS.") | 33 | | - pipeline.yields() | | 31 | + print("pipeline async run first time, SUSPEND.") | | 32 | + pipeline.suspend() | 34 | 33 |
| 35 | 34 | time.sleep(7.2) | 36 | 35 | print("pipeline async run, RESUME after 7200ms.") | @@ -41,4 +40,4 @@ def tutorial_cancel(): | 41 | 40 |
| 42 | 41 |
| 43 | 42 | if __name__ == '__main__': | 44 | | - tutorial_cancel() | | 43 | + tutorial_suspend() |
Collapse file Copy file name to clipboard File renamed without changes. Collapse file Copy file name to clipboardExpand all lines: src/GraphCtrl/GraphElement/GElement.cpp +10-6Lines changed: 10 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -246,12 +246,12 @@ CStatus GElement::fatProcessor(const CFunctionType& type) { | 246 | 246 | do { | 247 | 247 | status += isAsync() ? asyncRun() : run(); | 248 | 248 | /** | 249 | | - * 在实际run结束之后,首先需要判断一下是否进入yield状态了。 | | 249 | + * 在实际run结束之后,首先需要判断一下是否进入suspend状态了。 | 250 | 250 | * 接下来,如果状态是ok的,并且被条件hold住,则循环执行 | 251 | 251 | * 默认所有element的isHold条件均为false,即不hold,即执行一次 | 252 | 252 | * 可以根据需求,对任意element类型,添加特定的isHold条件 | 253 | 253 | * */ | 254 | | - } while (checkYield(), this->isHold() && status.isOK()); | | 254 | + } while (checkSuspend(), this->isHold() && status.isOK()); | 255 | 255 | doAspect(internal::GAspectType::FINISH_RUN, status); | 256 | 256 | } | 257 | 257 |
| @@ -431,10 +431,14 @@ CVoid GElement::dumpPerfInfo(std::ostream& oss) { | 431 | 431 | } | 432 | 432 |
| 433 | 433 |
| 434 | | -CVoid GElement::checkYield() { | 435 | | - std::unique_lock<std::mutex> lk(yield_mutex_); | 436 | | - this->yield_cv_.wait(lk, [this] { | 437 | | - return GElementState::YIELD != cur_state_.load(std::memory_order_acquire); | | 434 | +CVoid GElement::checkSuspend() { | | 435 | + if (GElementState::SUSPEND != cur_state_.load(std::memory_order_acquire)) { | | 436 | + return; | | 437 | + } | | 438 | + | | 439 | + std::unique_lock<std::mutex> lk(suspend_locker_.mtx_); | | 440 | + this->suspend_locker_.cv_.wait(lk, [this] { | | 441 | + return GElementState::SUSPEND != cur_state_.load(std::memory_order_acquire); | 438 | 442 | }); | 439 | 443 | } | 440 | 444 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/GraphCtrl/GraphElement/GElement.h +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -376,10 +376,10 @@ class GElement : public GElementObject, | 376 | 376 | CVoid dumpPerfInfo(std::ostream& oss); | 377 | 377 |
| 378 | 378 | /** | 379 | | - * 判断是否进入 yield状态。如果是的话,则等待恢复。未进入yield状态,则继续运行 | | 379 | + * 判断是否进入 suspend 状态。如果是的话,则等待恢复。未进入 suspend 状态,则继续运行 | 380 | 380 | * @return | 381 | 381 | */ | 382 | | - CVoid checkYield(); | | 382 | + CVoid checkSuspend(); | 383 | 383 |
| 384 | 384 | /** | 385 | 385 | * 判断当前元素,是否可以线性执行。默认返回true | @@ -469,8 +469,7 @@ class GElement : public GElementObject, | 469 | 469 |
| 470 | 470 | /** 异步执行相关信息 */ | 471 | 471 | std::future<CStatus> async_result_; // 用于记录当前节点的异步执行情况 | 472 | | - std::mutex yield_mutex_; // 控制停止执行的锁 | 473 | | - std::condition_variable yield_cv_; // 控制停止执行的条件变量 | | 472 | + UCvMutex suspend_locker_; // 控制停止执行锁信息 | 474 | 473 |
| 475 | 474 | friend class GNode; | 476 | 475 | friend class GGroup; | |
Collapse file Copy file name to clipboardExpand all lines: src/GraphCtrl/GraphElement/GElementDefine.h +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,7 +39,7 @@ enum class GElementType { | 39 | 39 | enum class GElementState { | 40 | 40 | NORMAL = 0x0000, // 正常状态 | 41 | 41 | CANCEL = 0x1001, // 取消状态 | 42 | | - YIELD = 0x1002, // 暂停状态 | | 42 | + SUSPEND = 0x1002, // 暂停状态(同 v3.0.0版本之前的 YIELD 含义相同) | 43 | 43 | TIMEOUT = 0x1010, // 超时状态 | 44 | 44 | }; | 45 | 45 | using GPipelineState = GElementState; // pipeline 同element共享状态 | |
Collapse file Copy file name to clipboardExpand all lines: src/GraphCtrl/GraphElement/GElementRepository.cpp +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -67,9 +67,9 @@ CStatus GElementRepository::pushAllState(const GElementState& state) { | 67 | 67 |
| 68 | 68 | for (auto& cur : elements_) { | 69 | 69 | cur->cur_state_.store(state, std::memory_order_release); | 70 | | - if (GElementState::YIELD != state) { | 71 | | - // 目前仅非yield状态,需要切换的。如果一直处于 yield状态,是不需要被通知的 | 72 | | - cur->yield_cv_.notify_one(); | | 70 | + if (GElementState::SUSPEND != state) { | | 71 | + // 目前仅非 suspend 状态,需要切换的。如果一直处于 suspend 状态,是不需要被通知的 | | 72 | + cur->suspend_locker_.cv_.notify_one(); | 73 | 73 | } | 74 | 74 | } | 75 | 75 | cur_state_ = state; // 记录当前的状态信息 | |
|