entropybit / slaprinter

A combinaton of an app and server for a sla 3d printer
0 stars 2 forks source link

Threading #2

Closed Aslan0 closed 9 years ago

Aslan0 commented 9 years ago

snes controller and beamer modules are running with the fpsclock of pygame. we should group them and put them on a single core, main.py on a second, (and lsitening deamosn/system on a third?)

entropybit commented 9 years ago

we can use the qt signal and slots together with the QThread class. You simply derive a class from QThread and every istanciated object of the derived class will run as thread, also a thread secure communication via signal and slots is possible.

See here https://wiki.python.org/moin/PyQt/Threading,_Signals_and_Slots

entropybit commented 9 years ago

Btw here is an simple example of two QThreads communicating with each other via signals

from PyQt4.QtCore import QThread, pyqtSignal

class Doer(QThread):
    signal = pyqtSignal()
    msg_signal = pyqtSignal(str, int, name='msg_signal')

    def __init__(self, parent = None):
        QThread.__init__(self, parent)
        self.counter = 0
        self.running = False

    @property
    def count(self):
        return self.counter

    def run(self):
        i = 0
        while True:
            self.counter = self.counter +1

            if self.counter % 1000 == 0:
                self.signal.emit()
                self.msg_signal.emit("message transmitted via signal",i )
            i = i+1
        # do something

class Waiter(QThread):
    def __init__(self, doer, parent = None):
        QThread.__init__(self, parent)
        self.doer = doer
        self.connect_with_doer()

    def connect_with_doer(self):
        if self.doer != None:
            self.doer.signal.connect(self.mod_signal)
            self.doer.msg_signal.connect(self.incomic_msg)

    def mod_signal(self):
        print("")
        print("modulus!")
        print("")

    def incomic_msg(self, msg, i):
        print("")
        print("MSG: " + str(msg) + " | loop count = " + str(i))
        print("")

    def run(self):
        # do something
        while True:
            pass

do = Doer()
wait = Waiter(do)
do.run()
Aslan0 commented 9 years ago

We havent used the QT framework on the printer so far. Do we have to? Cant we just use the thread class?

entropybit commented 9 years ago

Oh sry I haven't thought of that. No of course we don't have to use the QT Framework, we can also simply use python threads and then simply derive a class from threading and overwrite the run function so something like this


from threading import Threads

class ThreadedClass(Threads):
    def __ini__(self):
        Threads.init(self)

    def run(self):
        # do something here
        pass

t1 = ThreadedClass()
t1.start()
t1.stop()

start and stop do well exactly that they start or stop the thread where the overwritten run function will be called from within the start function.

For communication between the Threads you can use Ques and write some Message objects wrapping whatever Information you want to excchange between the Threads.

entropybit commented 9 years ago

Was it clear how to use Threading ? If so I think it would be a good step if you could "parallelize" the several functionalities we have within the sla_printer software by wrapping them into a Threading Object.

If the communication between the threads is your problem I can also provide some code on how to implement that.

entropybit commented 9 years ago

This is implemented now, a better solution would however be to use a multiprocessing Process instead of threading Thread, as these seem to enable actual parallel computation in python. Yet, as the functionality is essentially implemented as thought I will close this issue.