Why do I have to pass an instance of a class to a thread to modify its data, as opposed to passing the...
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
What is the id( ) function used for?
10 answers
Are lists thread-safe?
4 answers
I am creating a user interface and there are actions which take some time to complete.
The process fetches some data. I want to use that data in the Ui class.
To prevent the interface freezing I use have a worker object which I put in a separate thread and control using signals.
To get access to the data retrieved by the worker I pass the Ui object and have the worker update the content of the Ui classes data variable.
If I try to pass var directly and modify it in the worker thread, the content within the Ui class does not change. Why is this?
Code Example (This works):
from PyQt5 import QtCore
QtCore.Signal = QtCore.pyqtSignal
class threadController(QtCore.QObject):
fire = QtCore.Signal()
def __init__(self):
super().__init__()
class worker(QtCore.QObject):
finished = QtCore.Signal()
def __init__(self,ob):
super(worker,self).__init__()
self.ob = ob
def workerfunction(self):
self.ob.var = [1,2,3] #modify the variable
self.finished.emit()
class Ui():
def __init__(self):
self.thread1 = QtCore.QThread()
self.thread1.start()
self.var = #Create an empty variable
self.workerobj = worker(self)
self.workerobj.moveToThread(self.thread1)
self.threadControllerObj = threadController()
self.threadControllerObj.fire.connect(self.workerobj.workerfunction)
def startThreadProcess(self):
self.workerobj.finished.connect(self.check) #Need to make sure the thread has finished
self.threadControllerObj.fire.emit() #fire the worker function
def check(self):
print(self.var) #this runs when the worker function in the thread has finished
if __name__ == '__main__':
app = QtCore.QCoreApplication()
UiObj = Ui()
UiObj.startThreadProcess()
QtCore.QTimer.singleShot(1000, app.quit)
app.exec_()
In addition to this, is this the correct way to go about gaining access to data produced in a worker function which is running in a separate thread?
Code Example (This does not work)
from PyQt5 import QtCore
QtCore.Signal = QtCore.pyqtSignal
class threadController(QtCore.QObject):
fire = QtCore.Signal()
def __init__(self):
super().__init__()
class worker(QtCore.QObject):
finished = QtCore.Signal()
def __init__(self,var):
super(worker,self).__init__()
self.var = var
def workerfunction(self):
self.var = [1,2,3]
self.finished.emit()
class container():
def __init__(self):
self.thread1 = QtCore.QThread()
self.thread1.start()
self.var =
self.workerobj = worker(self.var)
self.workerobj.moveToThread(self.thread1)
self.threadControllerObj = threadController()
self.threadControllerObj.fire.connect(self.workerobj.workerfunction)
def startThreadProcess(self):
self.workerobj.finished.connect(self.check) #Need to make sure the thread has finished
self.threadControllerObj.fire.emit() #fire the worker function
def check(self):
print(self.var) #this runs when the worker function in the thread has finished
if __name__ == '__main__':
app = QtCore.QCoreApplication()
contain = container()
contain.startThreadProcess()
QtCore.QTimer.singleShot(1000, app.quit)
app.exec_()
python pyqt qthread
marked as duplicate by eyllanesc
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 15:12
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 8 more comments
This question already has an answer here:
What is the id( ) function used for?
10 answers
Are lists thread-safe?
4 answers
I am creating a user interface and there are actions which take some time to complete.
The process fetches some data. I want to use that data in the Ui class.
To prevent the interface freezing I use have a worker object which I put in a separate thread and control using signals.
To get access to the data retrieved by the worker I pass the Ui object and have the worker update the content of the Ui classes data variable.
If I try to pass var directly and modify it in the worker thread, the content within the Ui class does not change. Why is this?
Code Example (This works):
from PyQt5 import QtCore
QtCore.Signal = QtCore.pyqtSignal
class threadController(QtCore.QObject):
fire = QtCore.Signal()
def __init__(self):
super().__init__()
class worker(QtCore.QObject):
finished = QtCore.Signal()
def __init__(self,ob):
super(worker,self).__init__()
self.ob = ob
def workerfunction(self):
self.ob.var = [1,2,3] #modify the variable
self.finished.emit()
class Ui():
def __init__(self):
self.thread1 = QtCore.QThread()
self.thread1.start()
self.var = #Create an empty variable
self.workerobj = worker(self)
self.workerobj.moveToThread(self.thread1)
self.threadControllerObj = threadController()
self.threadControllerObj.fire.connect(self.workerobj.workerfunction)
def startThreadProcess(self):
self.workerobj.finished.connect(self.check) #Need to make sure the thread has finished
self.threadControllerObj.fire.emit() #fire the worker function
def check(self):
print(self.var) #this runs when the worker function in the thread has finished
if __name__ == '__main__':
app = QtCore.QCoreApplication()
UiObj = Ui()
UiObj.startThreadProcess()
QtCore.QTimer.singleShot(1000, app.quit)
app.exec_()
In addition to this, is this the correct way to go about gaining access to data produced in a worker function which is running in a separate thread?
Code Example (This does not work)
from PyQt5 import QtCore
QtCore.Signal = QtCore.pyqtSignal
class threadController(QtCore.QObject):
fire = QtCore.Signal()
def __init__(self):
super().__init__()
class worker(QtCore.QObject):
finished = QtCore.Signal()
def __init__(self,var):
super(worker,self).__init__()
self.var = var
def workerfunction(self):
self.var = [1,2,3]
self.finished.emit()
class container():
def __init__(self):
self.thread1 = QtCore.QThread()
self.thread1.start()
self.var =
self.workerobj = worker(self.var)
self.workerobj.moveToThread(self.thread1)
self.threadControllerObj = threadController()
self.threadControllerObj.fire.connect(self.workerobj.workerfunction)
def startThreadProcess(self):
self.workerobj.finished.connect(self.check) #Need to make sure the thread has finished
self.threadControllerObj.fire.emit() #fire the worker function
def check(self):
print(self.var) #this runs when the worker function in the thread has finished
if __name__ == '__main__':
app = QtCore.QCoreApplication()
contain = container()
contain.startThreadProcess()
QtCore.QTimer.singleShot(1000, app.quit)
app.exec_()
python pyqt qthread
marked as duplicate by eyllanesc
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 15:12
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
It doesn't look like you're directly passing invar
in the example above, is it your fixed code?
– Peter
Nov 23 '18 at 14:28
I'm not, that is a working example of passing the object, but If I try to pass just the var it doesn't work. I'll update the question to show these changes.
– Tim Mottram
Nov 23 '18 at 14:58
This is not modifying the object - it's assigning a new object to the namevar
in aworker
instance.
– BartoszKP
Nov 23 '18 at 15:10
Ok, but when check runs the value of var is correct in the first example, but not in the second example.
– Tim Mottram
Nov 23 '18 at 15:12
1
the instance saves the reference to the property, and this property stores the reference of the list, so if the list is replaced the data that stores the property, ie the reference to the list, will be updated with the new reference, it does not lose the reference, but in the second case it does not.
– eyllanesc
Nov 23 '18 at 19:49
|
show 8 more comments
This question already has an answer here:
What is the id( ) function used for?
10 answers
Are lists thread-safe?
4 answers
I am creating a user interface and there are actions which take some time to complete.
The process fetches some data. I want to use that data in the Ui class.
To prevent the interface freezing I use have a worker object which I put in a separate thread and control using signals.
To get access to the data retrieved by the worker I pass the Ui object and have the worker update the content of the Ui classes data variable.
If I try to pass var directly and modify it in the worker thread, the content within the Ui class does not change. Why is this?
Code Example (This works):
from PyQt5 import QtCore
QtCore.Signal = QtCore.pyqtSignal
class threadController(QtCore.QObject):
fire = QtCore.Signal()
def __init__(self):
super().__init__()
class worker(QtCore.QObject):
finished = QtCore.Signal()
def __init__(self,ob):
super(worker,self).__init__()
self.ob = ob
def workerfunction(self):
self.ob.var = [1,2,3] #modify the variable
self.finished.emit()
class Ui():
def __init__(self):
self.thread1 = QtCore.QThread()
self.thread1.start()
self.var = #Create an empty variable
self.workerobj = worker(self)
self.workerobj.moveToThread(self.thread1)
self.threadControllerObj = threadController()
self.threadControllerObj.fire.connect(self.workerobj.workerfunction)
def startThreadProcess(self):
self.workerobj.finished.connect(self.check) #Need to make sure the thread has finished
self.threadControllerObj.fire.emit() #fire the worker function
def check(self):
print(self.var) #this runs when the worker function in the thread has finished
if __name__ == '__main__':
app = QtCore.QCoreApplication()
UiObj = Ui()
UiObj.startThreadProcess()
QtCore.QTimer.singleShot(1000, app.quit)
app.exec_()
In addition to this, is this the correct way to go about gaining access to data produced in a worker function which is running in a separate thread?
Code Example (This does not work)
from PyQt5 import QtCore
QtCore.Signal = QtCore.pyqtSignal
class threadController(QtCore.QObject):
fire = QtCore.Signal()
def __init__(self):
super().__init__()
class worker(QtCore.QObject):
finished = QtCore.Signal()
def __init__(self,var):
super(worker,self).__init__()
self.var = var
def workerfunction(self):
self.var = [1,2,3]
self.finished.emit()
class container():
def __init__(self):
self.thread1 = QtCore.QThread()
self.thread1.start()
self.var =
self.workerobj = worker(self.var)
self.workerobj.moveToThread(self.thread1)
self.threadControllerObj = threadController()
self.threadControllerObj.fire.connect(self.workerobj.workerfunction)
def startThreadProcess(self):
self.workerobj.finished.connect(self.check) #Need to make sure the thread has finished
self.threadControllerObj.fire.emit() #fire the worker function
def check(self):
print(self.var) #this runs when the worker function in the thread has finished
if __name__ == '__main__':
app = QtCore.QCoreApplication()
contain = container()
contain.startThreadProcess()
QtCore.QTimer.singleShot(1000, app.quit)
app.exec_()
python pyqt qthread
This question already has an answer here:
What is the id( ) function used for?
10 answers
Are lists thread-safe?
4 answers
I am creating a user interface and there are actions which take some time to complete.
The process fetches some data. I want to use that data in the Ui class.
To prevent the interface freezing I use have a worker object which I put in a separate thread and control using signals.
To get access to the data retrieved by the worker I pass the Ui object and have the worker update the content of the Ui classes data variable.
If I try to pass var directly and modify it in the worker thread, the content within the Ui class does not change. Why is this?
Code Example (This works):
from PyQt5 import QtCore
QtCore.Signal = QtCore.pyqtSignal
class threadController(QtCore.QObject):
fire = QtCore.Signal()
def __init__(self):
super().__init__()
class worker(QtCore.QObject):
finished = QtCore.Signal()
def __init__(self,ob):
super(worker,self).__init__()
self.ob = ob
def workerfunction(self):
self.ob.var = [1,2,3] #modify the variable
self.finished.emit()
class Ui():
def __init__(self):
self.thread1 = QtCore.QThread()
self.thread1.start()
self.var = #Create an empty variable
self.workerobj = worker(self)
self.workerobj.moveToThread(self.thread1)
self.threadControllerObj = threadController()
self.threadControllerObj.fire.connect(self.workerobj.workerfunction)
def startThreadProcess(self):
self.workerobj.finished.connect(self.check) #Need to make sure the thread has finished
self.threadControllerObj.fire.emit() #fire the worker function
def check(self):
print(self.var) #this runs when the worker function in the thread has finished
if __name__ == '__main__':
app = QtCore.QCoreApplication()
UiObj = Ui()
UiObj.startThreadProcess()
QtCore.QTimer.singleShot(1000, app.quit)
app.exec_()
In addition to this, is this the correct way to go about gaining access to data produced in a worker function which is running in a separate thread?
Code Example (This does not work)
from PyQt5 import QtCore
QtCore.Signal = QtCore.pyqtSignal
class threadController(QtCore.QObject):
fire = QtCore.Signal()
def __init__(self):
super().__init__()
class worker(QtCore.QObject):
finished = QtCore.Signal()
def __init__(self,var):
super(worker,self).__init__()
self.var = var
def workerfunction(self):
self.var = [1,2,3]
self.finished.emit()
class container():
def __init__(self):
self.thread1 = QtCore.QThread()
self.thread1.start()
self.var =
self.workerobj = worker(self.var)
self.workerobj.moveToThread(self.thread1)
self.threadControllerObj = threadController()
self.threadControllerObj.fire.connect(self.workerobj.workerfunction)
def startThreadProcess(self):
self.workerobj.finished.connect(self.check) #Need to make sure the thread has finished
self.threadControllerObj.fire.emit() #fire the worker function
def check(self):
print(self.var) #this runs when the worker function in the thread has finished
if __name__ == '__main__':
app = QtCore.QCoreApplication()
contain = container()
contain.startThreadProcess()
QtCore.QTimer.singleShot(1000, app.quit)
app.exec_()
This question already has an answer here:
What is the id( ) function used for?
10 answers
Are lists thread-safe?
4 answers
python pyqt qthread
python pyqt qthread
edited Nov 23 '18 at 15:00
Tim Mottram
asked Nov 23 '18 at 14:10
Tim MottramTim Mottram
291516
291516
marked as duplicate by eyllanesc
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 15:12
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by eyllanesc
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 15:12
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
It doesn't look like you're directly passing invar
in the example above, is it your fixed code?
– Peter
Nov 23 '18 at 14:28
I'm not, that is a working example of passing the object, but If I try to pass just the var it doesn't work. I'll update the question to show these changes.
– Tim Mottram
Nov 23 '18 at 14:58
This is not modifying the object - it's assigning a new object to the namevar
in aworker
instance.
– BartoszKP
Nov 23 '18 at 15:10
Ok, but when check runs the value of var is correct in the first example, but not in the second example.
– Tim Mottram
Nov 23 '18 at 15:12
1
the instance saves the reference to the property, and this property stores the reference of the list, so if the list is replaced the data that stores the property, ie the reference to the list, will be updated with the new reference, it does not lose the reference, but in the second case it does not.
– eyllanesc
Nov 23 '18 at 19:49
|
show 8 more comments
It doesn't look like you're directly passing invar
in the example above, is it your fixed code?
– Peter
Nov 23 '18 at 14:28
I'm not, that is a working example of passing the object, but If I try to pass just the var it doesn't work. I'll update the question to show these changes.
– Tim Mottram
Nov 23 '18 at 14:58
This is not modifying the object - it's assigning a new object to the namevar
in aworker
instance.
– BartoszKP
Nov 23 '18 at 15:10
Ok, but when check runs the value of var is correct in the first example, but not in the second example.
– Tim Mottram
Nov 23 '18 at 15:12
1
the instance saves the reference to the property, and this property stores the reference of the list, so if the list is replaced the data that stores the property, ie the reference to the list, will be updated with the new reference, it does not lose the reference, but in the second case it does not.
– eyllanesc
Nov 23 '18 at 19:49
It doesn't look like you're directly passing in
var
in the example above, is it your fixed code?– Peter
Nov 23 '18 at 14:28
It doesn't look like you're directly passing in
var
in the example above, is it your fixed code?– Peter
Nov 23 '18 at 14:28
I'm not, that is a working example of passing the object, but If I try to pass just the var it doesn't work. I'll update the question to show these changes.
– Tim Mottram
Nov 23 '18 at 14:58
I'm not, that is a working example of passing the object, but If I try to pass just the var it doesn't work. I'll update the question to show these changes.
– Tim Mottram
Nov 23 '18 at 14:58
This is not modifying the object - it's assigning a new object to the name
var
in a worker
instance.– BartoszKP
Nov 23 '18 at 15:10
This is not modifying the object - it's assigning a new object to the name
var
in a worker
instance.– BartoszKP
Nov 23 '18 at 15:10
Ok, but when check runs the value of var is correct in the first example, but not in the second example.
– Tim Mottram
Nov 23 '18 at 15:12
Ok, but when check runs the value of var is correct in the first example, but not in the second example.
– Tim Mottram
Nov 23 '18 at 15:12
1
1
the instance saves the reference to the property, and this property stores the reference of the list, so if the list is replaced the data that stores the property, ie the reference to the list, will be updated with the new reference, it does not lose the reference, but in the second case it does not.
– eyllanesc
Nov 23 '18 at 19:49
the instance saves the reference to the property, and this property stores the reference of the list, so if the list is replaced the data that stores the property, ie the reference to the list, will be updated with the new reference, it does not lose the reference, but in the second case it does not.
– eyllanesc
Nov 23 '18 at 19:49
|
show 8 more comments
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
It doesn't look like you're directly passing in
var
in the example above, is it your fixed code?– Peter
Nov 23 '18 at 14:28
I'm not, that is a working example of passing the object, but If I try to pass just the var it doesn't work. I'll update the question to show these changes.
– Tim Mottram
Nov 23 '18 at 14:58
This is not modifying the object - it's assigning a new object to the name
var
in aworker
instance.– BartoszKP
Nov 23 '18 at 15:10
Ok, but when check runs the value of var is correct in the first example, but not in the second example.
– Tim Mottram
Nov 23 '18 at 15:12
1
the instance saves the reference to the property, and this property stores the reference of the list, so if the list is replaced the data that stores the property, ie the reference to the list, will be updated with the new reference, it does not lose the reference, but in the second case it does not.
– eyllanesc
Nov 23 '18 at 19:49