multithreading - QThread use in Python -
i new in writing bigger programs in python (i writing short scripts before). program i'm write recives data external device, saves database , displays in gui (continuously). since handling data time consuming want use threads (pyside qtcore qthreads specific). best way can think of set 2 threads, 1 database processes , 1 handling of serial port, gui running in mainthread. i've read whole bunch of stuff proper/improper qthreading, starting maya's post , digging deeper, this post found that:
when subclass , when not to?
if not need event loop in thread, should subclass. if need event loop , handle signals , slots within thread, may not need subclass.
question 1: first thing don't know way go (subclassing or not). guess subclassing, since (question2), i'm not entirely sure , i'm sticking movetothread()
.
question 2: other thing have no clue how make actions in threads event-driven (the serialthread receives data device, sends via signal databasethread, databasethread collects data , puts in database).
question 3: when using movetothread()
attributeerror: 'pyside.qtcore.qthread' object has no attribute 'my_awesome_method'
every method write class. it's clear me don't understand principle of operation here. how implement methods want my_class
have while using my_class.movetothread(my_thread)
?
i've found tutorials possible on qthreads , c++, discussion on qthreads in python interesting, not explain want know. need simple example signals, slots , exact explanation on how use run(). decorate it? why? how? how work?
the short version of code looks this:
from pyside import qtcore app.thrads.gui.gui import gui #my own class app.threads.db.db import database #my own class app.threads.serial.serial import serial #my own class class appmanager(): def __init__(self): self.gui = gui() self.db = database() self.db_thread = qtcore.qthread() self.db.movetothread(self.db_thread) self.db_thread.start() self.serial = serial() self.serial_thread = qtcore.qthread() self.serial.movetothread(self.serial_thread) self.serial_thread.start()
and database , serial class this:
from pyside import qtcore .db_signal import dbsignal class database(qtcore.qobject): def __init__(self): super(database, self).__init__() self.signal = dbsignal() def my_awesome_method(self): ''' awesome ''' pass
question 1
i think best way easiest: don't subclass, create 2 different threads. in first, move database object, in second serial one. it, won't make mistakes in implementation of sub-classed threads,and bug fixing quicker.
question 2
i don't know architecture of application, follows, after creating threads , moving worker objects in them:
self.serial_thread.started.connect(self.serial.start) self.serial_thread.finished.connect(self.serial.stop) self.db_thread.started.connect(self.db.start) self.serial_thread.finished.connect(self.db.stop) self.serial.data_ready.connect(self.db.handle_new_data) # starting database first, won't lose single packet of data self.db_thread.start() self.serial_thread.start()
in fact, advantage of qthreads doesn't modify code.
question 3
i think problem you're trying call my_awesome_method
qthread data base or serial listener has been moved to. on contrary, should call method object itself, if wasn't in different thread !
# bad! obj.movetothread(thread) thread.method_of_obj(param) # good! obj.movetothread(thread) obj.method_of_obj(param)
Comments
Post a Comment