better names

This commit is contained in:
blueloveTH 2025-04-13 23:31:44 +08:00
parent 01418a7144
commit b12d2bc4cc
4 changed files with 28 additions and 20 deletions

View File

@ -69,10 +69,10 @@ thread = ComputeThread(1)
print("Game Start") print("Game Start")
# import worldgen.py # import worldgen.py
thread.exec_blocked('from worldgen import gen_world') thread.exec('from worldgen import gen_world')
print("Submit WorldGen Job") 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 # wait for worldgen to finish
for i in range(1, 100000): for i in range(1, 100000):

View File

@ -35,9 +35,17 @@ class ComputeThread:
def last_error(self) -> str | None: ... def last_error(self) -> str | None: ...
def last_retval(self): ... def last_retval(self): ...
def exec(self, source: str) -> None: ... def submit_exec(self, source: str) -> None:
def eval(self, source: str) -> None: ... """Submit a job to execute some source code."""
def call(self, eval_src: str, *args, **kwargs) -> None: ...
def exec_blocked(self, source: str) -> None: ... def submit_eval(self, source: str) -> None:
def eval_blocked(self, source: str): ... """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."""

View File

@ -271,7 +271,7 @@ __ERROR:
return (c11_thrd_retval_t)0; 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); PY_CHECK_ARGC(2);
c11_ComputeThread* self = py_touserdata(py_arg(0)); c11_ComputeThread* self = py_touserdata(py_arg(0));
if(!self->is_done) return OSError("thread is not done yet"); 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; 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); PY_CHECK_ARGC(2);
c11_ComputeThread* self = py_touserdata(py_arg(0)); c11_ComputeThread* self = py_touserdata(py_arg(0));
if(!self->is_done) return OSError("thread is not done yet"); 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; 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); PY_CHECK_ARGC(4);
c11_ComputeThread* self = py_touserdata(py_arg(0)); c11_ComputeThread* self = py_touserdata(py_arg(0));
if(!self->is_done) return OSError("thread is not done yet"); if(!self->is_done) return OSError("thread is not done yet");
@ -383,7 +383,7 @@ __ERROR:
return false; 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); PY_CHECK_ARGC(2);
c11_ComputeThread* self = py_touserdata(py_arg(0)); c11_ComputeThread* self = py_touserdata(py_arg(0));
PY_CHECK_ARG_TYPE(1, tp_str); 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); 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); PY_CHECK_ARGC(2);
c11_ComputeThread* self = py_touserdata(py_arg(0)); c11_ComputeThread* self = py_touserdata(py_arg(0));
PY_CHECK_ARG_TYPE(1, tp_str); 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_error", ComputeThread_last_error);
py_bindmethod(type, "last_retval", ComputeThread_last_retval); 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, "exec", ComputeThread_exec);
py_bindmethod(type, "eval", ComputeThread_eval); 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() { void pk__add_module_pkpy() {

View File

@ -5,7 +5,7 @@ thread_1 = ComputeThread(1)
thread_2 = ComputeThread(2) thread_2 = ComputeThread(2)
for t in [thread_1, thread_2]: for t in [thread_1, thread_2]:
t.exec_blocked(''' t.exec('''
def func(a): def func(a):
from pkpy import currentvm from pkpy import currentvm
print("Hello from thread", currentvm(), "a =", a) print("Hello from thread", currentvm(), "a =", a)
@ -16,13 +16,13 @@ def func(a):
x = 123 x = 123
''') ''')
assert thread_1.eval_blocked('x') == 123 assert thread_1.eval('x') == 123
# thread_1.wait_for_done() # thread_1.wait_for_done()
# thread_2.wait_for_done() # thread_2.wait_for_done()
thread_1.call('func', [1, 2, 3]) thread_1.submit_call('func', [1, 2, 3])
thread_2.call('func', [4, 5, 6]) thread_2.submit_call('func', [4, 5, 6])
while not thread_1.is_done or not thread_2.is_done: while not thread_1.is_done or not thread_2.is_done:
print("Waiting for threads to finish...") print("Waiting for threads to finish...")