Page 2 of 2

Re: Global functions?

Posted: Thu Sep 21, 2017 11:13 am
by kgschlosser
you cannot start it more then one time. you will have to create a new instance if you want to run it again.

here is a code block of something you can start and stop as you like. but you can still only have one running copy of it at a time.

Code: Select all

import threading

class ThreadClass(object):

    def __init__(self):
        self._event = threading.Event()
        self._thread = None

    def start(self, param1, param2):
        if self._thread is None:
            self._thread = threading.Thread(
                name='SomeThreadName',
                target=self.run,
                args=(param1, param2)
            )
            self._thread.start()

    def run(self, param1, param2):
        eg.Bind('Main.OnClose', self.stop)
        # startup code
        print 'Thread started'
        print str(param1)
        print str(param2)
        x = 1
        # looping code
        while not self._event.isSet():
            print x
            x += 1
            if x == param2:
                self._event.set()
            else:
                 # non blocking sleep
                self._event.wait(param1)

        # closing code
        eg.Unbind('Main.OnClose', self.stop)
        print 'Thread closed'

    def stop(self, dummyEvent=None):
        if self._thread is not None:
            self._event.set()
            self._thread.join(1.0)
        return False

Re: Global functions?

Posted: Thu Sep 21, 2017 12:36 pm
by Luca Brasi
@kgschlosser
Dude, can you give me a hint why I can't run the thread twice? I'm trying to figure it out myself but I have the feeling I will have to do some online courses on threading and classes first...

Re: Global functions?

Posted: Thu Sep 21, 2017 1:11 pm
by Luca Brasi
wtf :-)
I had this page open for some time before asking you for help. You must have posted the exact same thing I was asking for in the meantime...
Thanks so much. I'll check it out soon

Re: Global functions?

Posted: Thu Sep 21, 2017 6:33 pm
by kgschlosser
you can run it twice if you want to have it so you can run multiple instances at the same time then you would need to do

eg.globals.ThreadClass = ThreadClass

and then you can create as many threads as you like by doing


eg.globals.thread_1 = eg.globals.ThreadClass()
eg.globals.thread_1.start(1.0, 10)

eg.globals.thread_2 = eg.globals.ThreadClass()
eg.globals.thread_2.start(1.0, 10)

eg.globals.thread_3 = eg.globals.ThreadClass()
eg.globals.thread_3.start(1.0, 10)

eg.globals.thread_4 = eg.globals.ThreadClass()
eg.globals.thread_4.start(1.0, 10)

eg.globals.thread_5 = eg.globals.ThreadClass()
eg.globals.thread_5.start(1.0, 10)


by doing eg.globals.ThreadClass = ThreadClass you are setting the class to a global not the instance. so at that point you can create an instance from the global you made for the class

Re: Global functions?

Posted: Thu Sep 21, 2017 6:52 pm
by kgschlosser
next lesson is going to be about how to pass data to the thread for it to process so you won't have to keep on creating threads to do things. you can keep the thread idle until some data comes in.

Code: Select all

import threading

class ThreadClass(object):

    def __init__(self):
        self._event = threading.Event()
        self._thread = None
        self._queue_lock = threading.Lock()
        self._queue_event = threading.Event()
        self._queue = []

    def process_data(self, param1, param2):
        self._queue_lock.acquire()
        self._queue += [(param1, param2)]
        self._queue_lock.release()
        self._queue_event.set()


    def start(self):
        if self._thread is None:
            self._queue_event.clear()
            self._event.clear()
            del self._queue[:]

            self._thread = threading.Thread(
                name='SomeThreadName',
                target=self.run
            )
            self._thread.start()

    def run(self):
        eg.Bind('Main.OnClose', self.stop)
        # startup code
        print 'Thread started'

        # looping code
        while not self._event.isSet():
            self._queue_event.wait()
            self._queue_event.clear()
            while not self._event.isSet() and self._queue:
                self._queue_lock.acquire()
                param1, param2 = self._queue.pop(0)
                self._queue_lock.release()
                print "processing param1", param1
                print "processing param2", param2

        # closing code
        eg.Unbind('Main.OnClose', self.stop)
        self._thread = None
        print 'Thread closed'

    def stop(self, dummyEvent=None):
        if self._thread is not None:
            self._event.set()
            self._queue_event.set()
            self._thread.join(1.0)
        return False

t = ThreadClass()
t.start()
t.process_data(1.0, 10)
t.process_data(5.0, 30)
t.process_data(200, 60)


# if you call t.stop() right here you will notice that it doesn't complete
# printing the 3 process_data's this is because they are queued to be processed
# and haven't been called up yet. and close is something that is of higher
# priority so it closes the thread even tho the data has not been processed.

# t.stop()

Re: Global functions?

Posted: Thu Oct 05, 2017 11:06 am
by Luca Brasi
Sorry for not replying earlier here. I somehow missed that one. I'll check out the previous posts with the treading code now and will try to understand them completely before proceeding with your last post :-)