Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Global functions?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Global functions?

Post 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
If you like the work I have been doing then feel free to Image
User avatar
Luca Brasi
Experienced User
Posts: 262
Joined: Sat Oct 11, 2008 12:39 pm

Re: Global functions?

Post 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...
Win10 x64 Prof. / Eventghost latest / MCE Plugin / MCE RC6 Receiver / Logitech Harmony Hub / MediaPortal / kodi
User avatar
Luca Brasi
Experienced User
Posts: 262
Joined: Sat Oct 11, 2008 12:39 pm

Re: Global functions?

Post 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
Win10 x64 Prof. / Eventghost latest / MCE Plugin / MCE RC6 Receiver / Logitech Harmony Hub / MediaPortal / kodi
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Global functions?

Post 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
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
Posts: 5190
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Global functions?

Post 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()
If you like the work I have been doing then feel free to Image
User avatar
Luca Brasi
Experienced User
Posts: 262
Joined: Sat Oct 11, 2008 12:39 pm

Re: Global functions?

Post 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 :-)
Win10 x64 Prof. / Eventghost latest / MCE Plugin / MCE RC6 Receiver / Logitech Harmony Hub / MediaPortal / kodi
Post Reply