0

I'm using the _thread library of micropython and in a class I'm coding to handdle tasks in a thread when I'm trying to get if thread is working i receibe the error Code is unreachable error

This is my code:

import _thread
from time import sleep

def task_0():
    print('c')
    sleep(0.5)
    print('o')
    sleep(0.5)
    print('r')
    sleep(0.5)
    print('e')
    sleep(0.5)
    print('0')
    sleep(0.5)

def task_1():
    print('C')
    sleep(0.5)
    print('O')
    sleep(0.5)
    print('R')
    sleep(0.5)
    print('E')
    sleep(0.5)
    print('1')
    sleep(0.5)

class ThreadWorker:
    def __init__(self):
        self.threadId = _thread.get_ident()
        self.lock = _thread.allocate_lock()
        self.queue = []

    def add(self, task):
        self.queue.append(task())

    def status(self):
        if _thread.get_ident() is None:
            print('Not thread working')
        else:
            print('Thread ID: {}'.format(_thread.get_ident()))

    def start(self):
        while len(self.queue) > 0:
            fn = self.queue.pop(0)
            if fn is None:
                break

            self.lock.acquire()
            print("Queue: {}\n Next Task: {}".format(self.queue, fn))
            self.threadId = _thread.start_new_thread(fn, ())
            self.lock.release()

    def stop(self):
        print('Trying to stop Thread with ID: {}'.format(self.threadId))
        _thread.exit()


thread = ThreadWorker()
thread.start()
thread.add(task_0)
thread.add(task_1)
thread.stop()
thread.status() <--- (method) status: () -> None | Code is unreachablePylance

I'm trying to stop the thread when all tasks are done and show the tread information.

0 Answers0