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.
@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...
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
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
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.
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
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