Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am looking for a solution for _thread in class object
#1

Hi,

This is an example of
threading

for a class...

[python]

# A minimal threading example with an object
import threading
import time


class MyThread(threading.Thread):
    def run(self):                                        # Default called function with mythread.start()
        print("{} started!".format(self.getName()))        # "Thread-x started!"
        time.sleep(1)                                      # Pretend to work for a second
        print("{} finished!".format(self.getName()))      # "Thread-x finished!"

def main():
    for x in range(4):                                    # Four times...
        mythread = MyThread(name = "Thread-{}".format(x))  # ...Instantiate a thread and pass a unique ID to it
        mythread.start()                                  # ...Start the thread, run method will be invoked
        time.sleep(.9)                                    # ...Wait 0.9 seconds before starting another

if __name__ == '__main__':
    main()[/python]


Not work With Lobo Micropython, I am looking for a solution for _thread..

[python]
import _thread
import time

...[/python]


class MyThread(_thread.Thread):  ? :|




Reply
#2

I found this module in internet:





threading.py

[python]

import _thread


class Thread:

    def __init__(self, group=None, target=None, name=None, args=(), kwargs=None):
        self.target = target
        self.args = args
        self.kwargs = {} if kwargs is None else kwargs

    def start(self):
        _thread.start_new_thread(self.run, ())

    def run(self):
        self.target(*self.args, **self.kwargs)[/python]


But not work with this example code...


Traceback (most recent call last):
  File "main.py", line 237, in <module>
  File "threading.py", line 12, in start
TypeError: function missing 1 required positional arguments

[python]

[File "main.py", line 237, in <module>]

ERROR IN  >>>
mythread.start()                                  # ...Start the thread, run method will be invoked


[File "threading.py", line 12, in start]

>>>  
_thread.start_new_thread(self.run, ())



[/python]



Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)