Skip to content

What is the point of the threading? #1

Description

@fake-name

In https://github.com/raffmont/pynextion/blob/master/pynextion/__init__.py


    def nxRead(self, cmax=0, timeout=0):
        s = []
        done = False

        def _reader():
            count = 0
            time_now = time.clock()
            while timeout == 0 or (time.clock() - time_now) < timeout:
                r = self.ser.read()
                if r is None or r == "":
                    continue

                c = ord(r)
                if c == 0xff and len(s) == 0:
                    continue

                if c != 0x00:
                    if self.debug is True:
                        print("\/ :" + str(c) + ":" + str(len(s)) + ":" +
                              str(count))

                    s.append(c)
                    if len(s) == cmax:
                        return
                    if c == 0xff:
                        count = count + 1
                        if count == 3:
                            if self.debug is True:
                                print("!!")
                            return
                    else:
                        count = 0
                    if self.debug is True:
                        print("/\ :" + str(c) + ":" + str(len(s)) + ":" +
                              str(count))
            print("Timeout")

        t = Thread(target=_reader)
        t.start()
        t.join()
        return s

You're starting a separate thread to read from the com port, and then blocking until it completes.

Why is it even there? It's literally equivalent to just calling _reader() in the context of the main thread.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions