检查线程是否已在MicroPython中启动

时间:2019-05-28 19:17:54

标签: python python-multithreading micropython

我只是在LEGO EV3单元上试用了一些MicroPython脚本,并且努力寻找任何文档/示例来告诉我如何检查线程是否正在运行。

def newMethod():
    print("new method")
t1 = threading.Thread(target=newMethod)
while True:
    ...
    if t1.is_alive() is False:
        t1.start()

现在,除了测试是否已启动t1之外,此方法都有效。但它是Python2.7。值得庆幸的是,除了is_alive之外,大多数代码都可以在microPython中使用?我收到错误消息。

Traceback (most recent call last):
  File "./micro.py", line 79, in <module>
  File "./micro.py", line 73, in <module>
AttributeError: 'Thread' object has no attribute 'is_alive'

我需要手动跟踪吗?还是有像python 2.7这样的内置方法

1 个答案:

答案 0 :(得分:1)

当前没有为任何支持线程的端口实现

is_alive(在官方存储库中,不知道其他端口)。因此,您的选择是:

  • 手动跟踪
  • 在代码中使用不同的逻辑,因此您不需要它
  • create a feature request
  • 在您使用的端口上自行实现(如果您知道C,这可能相对容易,因为线程实现通常具有内置的方式来报告其状态)

对于最后一种情况,我很好奇并提出了建议,对于unix端口,因为我没有Lego可以玩,这为is_alive模块添加了_thread。从那里开始,很容易将它放入您的Threading类中。请注意,这只是为了让您了解所需的内容,并且未经验证的概念证明YMMV。补丁:

--- a/ports/unix/mpthreadport.c
+++ b/ports/unix/mpthreadport.c
@@ -174,7 +174,20 @@ void mp_thread_start(void) {
     pthread_mutex_unlock(&thread_mutex);
 }

-void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {
+int mp_thread_is_alive(void* id) {
+    int ready = 0;
+    pthread_mutex_lock(&thread_mutex);
+    for (thread_t *th = thread; th != NULL; th = th->next) {
+        if (th->id == (pthread_t)id && th->ready) {
+            ready = 1;
+            break;
+        }
+    }
+    pthread_mutex_unlock(&thread_mutex);
+    return ready;
+}
+
+void* mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {
     // default stack size is 8k machine-words
     if (*stack_size == 0) {
         *stack_size = 8192 * BYTES_PER_WORD;
@@ -225,7 +238,7 @@ void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {

     pthread_mutex_unlock(&thread_mutex);

-    return;
+    return (void*) th->id;

 er:
     mp_raise_OSError(ret);
diff --git a/py/modthread.c b/py/modthread.c
index 91237a7..246dd5c 100644
--- a/py/modthread.c
+++ b/py/modthread.c
@@ -264,9 +264,7 @@ STATIC mp_obj_t mod_thread_start_new_thread(size_t n_args, const mp_obj_t *args)
     th_args->fun = args[0];

     // spawn the thread!
-    mp_thread_create(thread_entry, th_args, &th_args->stack_size);
-
-    return mp_const_none;
+    return mp_obj_new_int_from_uint((uintptr_t)mp_thread_create(thread_entry, th_args, &th_args->stack_size));
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_thread_start_new_thread_obj, 2, 3, mod_thread_start_new_thread);

@@ -275,6 +273,11 @@ STATIC mp_obj_t mod_thread_exit(void) {
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_thread_exit_obj, mod_thread_exit);

+STATIC mp_obj_t mod_thread_is_alive(mp_obj_t id) {
+    return mp_obj_new_bool(mp_thread_is_alive((void*) mp_obj_get_int(id)));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_thread_is_alive_obj, mod_thread_is_alive);
+
 STATIC mp_obj_t mod_thread_allocate_lock(void) {
     return MP_OBJ_FROM_PTR(mp_obj_new_thread_lock());
 }
@@ -287,6 +290,7 @@ STATIC const mp_rom_map_elem_t mp_module_thread_globals_table[] = {
     { MP_ROM_QSTR(MP_QSTR_stack_size), MP_ROM_PTR(&mod_thread_stack_size_obj) },
     { MP_ROM_QSTR(MP_QSTR_start_new_thread), MP_ROM_PTR(&mod_thread_start_new_thread_obj) },
     { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&mod_thread_exit_obj) },
+    { MP_ROM_QSTR(MP_QSTR_is_alive), MP_ROM_PTR(&mod_thread_is_alive_obj) },
     { MP_ROM_QSTR(MP_QSTR_allocate_lock), MP_ROM_PTR(&mod_thread_allocate_lock_obj) },
 };

diff --git a/py/mpthread.h b/py/mpthread.h
index 602df83..46f1a3a 100644
--- a/py/mpthread.h
+++ b/py/mpthread.h
@@ -40,7 +40,8 @@ struct _mp_state_thread_t;

 struct _mp_state_thread_t *mp_thread_get_state(void);
 void mp_thread_set_state(void *state);
-void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size);
+void* mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size);
+int mp_thread_is_alive(void*);

启用以下代码:

import _thread
import utime

def Entry():
    utime.sleep_ms(1)

t = _thread.start_new_thread(Entry, ())
for i in range(10):
    print('thread alive', _thread.is_alive(t))

打印两次,然后打印False。