From b12d2bc4ccf9b356e407e9162c4b8bde3949f8a3 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 13 Apr 2025 23:31:44 +0800 Subject: [PATCH] better names --- docs/features/threading.md | 4 ++-- include/typings/pkpy.pyi | 18 +++++++++++++----- src/modules/pkpy.c | 18 +++++++++--------- tests/98_thread.py | 8 ++++---- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/docs/features/threading.md b/docs/features/threading.md index a51857de..de12ebc5 100644 --- a/docs/features/threading.md +++ b/docs/features/threading.md @@ -69,10 +69,10 @@ thread = ComputeThread(1) print("Game Start") # import worldgen.py -thread.exec_blocked('from worldgen import gen_world') +thread.exec('from worldgen import gen_world') print("Submit WorldGen Job") -thread.call('gen_world', 3, (100, 100), 10) +thread.submit_call('gen_world', 3, (100, 100), 10) # wait for worldgen to finish for i in range(1, 100000): diff --git a/include/typings/pkpy.pyi b/include/typings/pkpy.pyi index a41581c0..ec5815af 100644 --- a/include/typings/pkpy.pyi +++ b/include/typings/pkpy.pyi @@ -35,9 +35,17 @@ class ComputeThread: def last_error(self) -> str | None: ... def last_retval(self): ... - def exec(self, source: str) -> None: ... - def eval(self, source: str) -> None: ... - def call(self, eval_src: str, *args, **kwargs) -> None: ... + def submit_exec(self, source: str) -> None: + """Submit a job to execute some source code.""" - def exec_blocked(self, source: str) -> None: ... - def eval_blocked(self, source: str): ... + def submit_eval(self, source: str) -> None: + """Submit a job to evaluate some source code.""" + + def submit_call(self, eval_src: str, *args, **kwargs) -> None: + """Submit a job to call a function with arguments.""" + + def exec(self, source: str) -> None: + """Directly execute some source code.""" + + def eval(self, source: str): + """Directly evaluate some source code.""" diff --git a/src/modules/pkpy.c b/src/modules/pkpy.c index 160b54b4..79cac762 100644 --- a/src/modules/pkpy.c +++ b/src/modules/pkpy.c @@ -271,7 +271,7 @@ __ERROR: return (c11_thrd_retval_t)0; } -static bool ComputeThread_exec(int argc, py_Ref argv) { +static bool ComputeThread_submit_exec(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_ComputeThread* self = py_touserdata(py_arg(0)); if(!self->is_done) return OSError("thread is not done yet"); @@ -294,7 +294,7 @@ static bool ComputeThread_exec(int argc, py_Ref argv) { return true; } -static bool ComputeThread_eval(int argc, py_Ref argv) { +static bool ComputeThread_submit_eval(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_ComputeThread* self = py_touserdata(py_arg(0)); if(!self->is_done) return OSError("thread is not done yet"); @@ -317,7 +317,7 @@ static bool ComputeThread_eval(int argc, py_Ref argv) { return true; } -static bool ComputeThread_call(int argc, py_Ref argv) { +static bool ComputeThread_submit_call(int argc, py_Ref argv) { PY_CHECK_ARGC(4); c11_ComputeThread* self = py_touserdata(py_arg(0)); if(!self->is_done) return OSError("thread is not done yet"); @@ -383,7 +383,7 @@ __ERROR: return false; } -static bool ComputeThread_exec_blocked(int argc, py_Ref argv) { +static bool ComputeThread_exec(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_ComputeThread* self = py_touserdata(py_arg(0)); PY_CHECK_ARG_TYPE(1, tp_str); @@ -391,7 +391,7 @@ static bool ComputeThread_exec_blocked(int argc, py_Ref argv) { return c11_ComputeThread__exec_blocked(self, source, EXEC_MODE); } -static bool ComputeThread_eval_blocked(int argc, py_Ref argv) { +static bool ComputeThread_eval(int argc, py_Ref argv) { PY_CHECK_ARGC(2); c11_ComputeThread* self = py_touserdata(py_arg(0)); PY_CHECK_ARG_TYPE(1, tp_str); @@ -409,12 +409,12 @@ static void pk_ComputeThread__register(py_Ref mod) { py_bindmethod(type, "last_error", ComputeThread_last_error); py_bindmethod(type, "last_retval", ComputeThread_last_retval); + py_bindmethod(type, "submit_exec", ComputeThread_submit_exec); + py_bindmethod(type, "submit_eval", ComputeThread_submit_eval); + py_bind(py_tpobject(type), "submit_call(self, eval_src, *args, **kwargs)", ComputeThread_submit_call); + py_bindmethod(type, "exec", ComputeThread_exec); py_bindmethod(type, "eval", ComputeThread_eval); - py_bind(py_tpobject(type), "call(self, eval_src, *args, **kwargs)", ComputeThread_call); - - py_bindmethod(type, "exec_blocked", ComputeThread_exec_blocked); - py_bindmethod(type, "eval_blocked", ComputeThread_eval_blocked); } void pk__add_module_pkpy() { diff --git a/tests/98_thread.py b/tests/98_thread.py index 45db99f0..bc428315 100644 --- a/tests/98_thread.py +++ b/tests/98_thread.py @@ -5,7 +5,7 @@ thread_1 = ComputeThread(1) thread_2 = ComputeThread(2) for t in [thread_1, thread_2]: - t.exec_blocked(''' + t.exec(''' def func(a): from pkpy import currentvm print("Hello from thread", currentvm(), "a =", a) @@ -16,13 +16,13 @@ def func(a): x = 123 ''') -assert thread_1.eval_blocked('x') == 123 +assert thread_1.eval('x') == 123 # thread_1.wait_for_done() # thread_2.wait_for_done() -thread_1.call('func', [1, 2, 3]) -thread_2.call('func', [4, 5, 6]) +thread_1.submit_call('func', [1, 2, 3]) +thread_2.submit_call('func', [4, 5, 6]) while not thread_1.is_done or not thread_2.is_done: print("Waiting for threads to finish...")