mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
update _thread pacakge
This commit is contained in:
parent
eed2e11a20
commit
c488eaab7d
17
examples/_thread/test2.py
Normal file
17
examples/_thread/test2.py
Normal file
@ -0,0 +1,17 @@
|
||||
import time
|
||||
import _thread
|
||||
|
||||
finished = False
|
||||
|
||||
def test_thread():
|
||||
global finished
|
||||
for i in range(3):
|
||||
print(i)
|
||||
time.sleep(0.1)
|
||||
finished = True
|
||||
|
||||
# 开启线程 获取数据
|
||||
_thread.start_new_thread(test_thread, ())
|
||||
while not finished:
|
||||
time.sleep(0.1)
|
||||
time.sleep(0.1)
|
@ -25,25 +25,46 @@ static void _thread_func(void* arg) {
|
||||
pika_GIL_ENTER();
|
||||
PikaObj* ctx = New_TinyObj(NULL);
|
||||
pika_thread_info* info = (pika_thread_info*)arg;
|
||||
obj_setArg(ctx, "args", info->args);
|
||||
if (NULL != info->args) {
|
||||
obj_setArg(ctx, "args", info->args);
|
||||
}
|
||||
obj_setArg(ctx, "thread", info->function);
|
||||
/* clang-format off */
|
||||
PIKA_PYTHON(
|
||||
thread(*args)
|
||||
)
|
||||
/* clang-format on */
|
||||
const uint8_t bytes[] = {
|
||||
0x0c, 0x00, 0x00, 0x00, /* instruct array size */
|
||||
0x20, 0x81, 0x01, 0x00, 0x10, 0x08, 0x06, 0x00, 0x00, 0x02, 0x08, 0x00,
|
||||
/* instruct array */
|
||||
0x0f, 0x00, 0x00, 0x00, /* const pool size */
|
||||
0x00, 0x61, 0x72, 0x67, 0x73, 0x00, 0x2a, 0x00, 0x74, 0x68, 0x72, 0x65,
|
||||
0x61, 0x64, 0x00, /* const pool */
|
||||
};
|
||||
pikaVM_runByteCode(ctx, (uint8_t*)bytes);
|
||||
|
||||
if (NULL == info->args) {
|
||||
/* clang-format off */
|
||||
PIKA_PYTHON(
|
||||
thread()
|
||||
)
|
||||
/* clang-format on */
|
||||
const uint8_t bytes[] = {
|
||||
0x04, 0x00, 0x00, 0x00, /* instruct array size */
|
||||
0x00, 0x82, 0x01, 0x00, /* instruct array */
|
||||
0x08, 0x00, 0x00, 0x00, /* const pool size */
|
||||
0x00, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x00, /* const pool */
|
||||
};
|
||||
pikaVM_runByteCode(ctx, (uint8_t*)bytes);
|
||||
} else {
|
||||
/* clang-format off */
|
||||
PIKA_PYTHON(
|
||||
thread(*args)
|
||||
)
|
||||
/* clang-format on */
|
||||
const uint8_t bytes[] = {
|
||||
0x0c, 0x00, 0x00, 0x00, /* instruct array size */
|
||||
0x20, 0x81, 0x01, 0x00, 0x10, 0x08, 0x06, 0x00, 0x00, 0x02, 0x08,
|
||||
0x00,
|
||||
/* instruct array */
|
||||
0x0f, 0x00, 0x00, 0x00, /* const pool size */
|
||||
0x00, 0x61, 0x72, 0x67, 0x73, 0x00, 0x2a, 0x00, 0x74, 0x68, 0x72,
|
||||
0x65, 0x61, 0x64, 0x00, /* const pool */
|
||||
};
|
||||
pikaVM_runByteCode(ctx, (uint8_t*)bytes);
|
||||
}
|
||||
obj_deinit(ctx);
|
||||
arg_deinit(info->function);
|
||||
arg_deinit(info->args);
|
||||
if (NULL != info->args) {
|
||||
arg_deinit(info->args);
|
||||
}
|
||||
pika_debug("thread exiting");
|
||||
pika_platform_thread_t* thread = info->thread;
|
||||
pikaFree(info, sizeof(pika_thread_info));
|
||||
@ -56,11 +77,19 @@ static void _thread_func(void* arg) {
|
||||
#endif
|
||||
}
|
||||
|
||||
int PikaStdData_Tuple_len(PikaObj* self);
|
||||
|
||||
void _thread_start_new_thread(PikaObj* self, Arg* function, Arg* args_) {
|
||||
pika_thread_info* info =
|
||||
(pika_thread_info*)pikaMalloc(sizeof(pika_thread_info));
|
||||
pika_platform_memset(info, 0, sizeof(pika_thread_info));
|
||||
info->function = arg_copy(function);
|
||||
info->args = arg_copy(args_);
|
||||
|
||||
PikaObj* tuple = arg_getPtr(args_);
|
||||
size_t tuple_size = PikaStdData_Tuple_len(tuple);
|
||||
if (tuple_size > 0) {
|
||||
info->args = arg_copy(args_);
|
||||
}
|
||||
_VM_lock_init();
|
||||
info->thread = pika_platform_thread_init(
|
||||
"pika_thread", _thread_func, info, PIKA_THREAD_STACK_SIZE,
|
||||
|
Loading…
x
Reference in New Issue
Block a user