1

I have a worker object, which has many functions and I'll like to use them on an extra thread. So, I followed this answer and it works if I pass a fixed number of arguments to the functions, more or less like this:

    _signalCommand = QtCore.pyqtSignal(str, str)
    self._signalCommand.connect(self.nanomax.test)
    self._signalCommand.emit('testA', 'testB')

But in my case, I can't use a fixed number of arguments. I need optional ones, my functions are like this:

 def test(self, test1='test1', test2='test2'):
    print test1
    print test2

So, in order to fix that I was using lambda, like this:

    _signalCommand = QtCore.pyqtSignal()
    self._signalCommand.connect(lambda: self.nanomax.test('testA', 'testB')
    self._signalCommand.emit()

Doing this way, indeed, fix my optional arguments problems but it makes the object running in the main thread. I think this is because lambda creates another function and is like calling the worker directly (self.nanomax.test('testA', 'testB') as mentioned in the answer I previously shared.

Is there anyway to avoid this? Thank you in advance.

Community
  • 1
  • 1
Eduardo
  • 631
  • 1
  • 12
  • 25
  • In the case you describe here, where you have two optional arguments and you know their value in advance, I do not see any problem. Just create a signal `_signalCommand = QtCore.pyqtSignal()` and another one `_signalCommand2 = QtCore.pyqtSignal(str, str)` connect them both and emit whichever you like depending on if you want to use the default values or not. – ImportanceOfBeingErnest Nov 09 '16 at 21:35
  • The thing is that in my program I need got get all optional arguments coming from a list. This list would bring me any number of parameters with any values. – Eduardo Nov 11 '16 at 10:23

1 Answers1

0

You could probably use partial for this

eg :

from functools import partial
...

_signalCommand = QtCore.pyqtSignal()
    self._signalCommand.connect(partial(self.nanomax.test, 'testA', 'testB'))
    self._signalCommand.emit()
Achayan
  • 5,720
  • 2
  • 39
  • 61
  • Thanks, it seams it is working but the only problem is that I can't pass the list values using the unpacking operator like this: `self._signalCommand.connect(partial(self.nanomax.test, *self.arg[1:]))`, is there another way to do that? Pass as many arguments as the list goes? – Eduardo Nov 10 '16 at 11:43