0

I'm having trouble using the starred expressions in combination with fixed argument lists when attempting to create threads.

Consider the following code:

the_queue = Queue()

def do_something(arg1, arg2, queue):
    # Do some stuff...
    result = arg1 + arg2

    queue.put(result)

def init_thread(*arguments):
    t = Thread(target=do_something, args=(arguments, the_queue))
    t.start()
    t.join()

init_thread(3,6)

This throws the exception:

TypeError: do_something() takes exactly 3 arguments (2 given)

In other words, the "arguments" tuple is evaluated as one tuple object (i.e. it's not unpacked), and the_queue is regarded as the second argument.

The code needs to be able to initalize threads calling different methods with an unknown number of arguments, but that always will have a "queue" parameter at the end.

Is there any way to achieve this? In that case, how? And if not -- what am I doing wrong?

Thanks.

EDIT: I should add that calling the "init_thread()" method with the queue as an argument is not an option, since I don't want the rest of my code to be "aware" of how the thread handler works internally...

Atra Azami
  • 2,215
  • 1
  • 14
  • 12

2 Answers2

1

You need to make a new tuple.

t = Thread(target = do_something, args = arguments + (the_queue, ))
cha0site
  • 10,517
  • 3
  • 33
  • 51
1

You can also unpack the packed-up *arguments tuple, like so:

>>> def Print(*args):
...     print('I am prepended to every message!', *args)
... 
>>> Print('This', 'has', 'four', 'arguments')
I am prepended to every message! This has four arguments
>>> def Print(*args):
...     print('I am prepended to every message!', args)
... 
>>> Print('This', 'has', 'four', 'arguments')
I am prepended to every message! ('This', 'has', 'four', 'arguments')

So you see, referring to *agruments, as opposed to arguments, will unpack the tuple. Thus your code might be:

def init_thread(*arguments):  
    t = Thread(target=do_something, args=(*arguments, the_queue))
    t.start()
    t.join()
Dubslow
  • 553
  • 2
  • 6
  • 15
  • I actually tried writing it that way first (with *arguments to unpack) but I get an error when trying to do it. I've tried it in python 2.6 and python 3.2.2. In which version of Python is that legal? It certainly is a useful feature which I thought was missing when facing my problem... Edit: The error is "can use starred expression only as assignment target" – Atra Azami Jul 10 '12 at 20:40
  • I have no idea. I use Python 3.2. A quick Google turned up some similar problems here. http://stackoverflow.com/questions/3870778/why-does-this-cause-a-syntax-error http://stackoverflow.com/questions/10967819/python-when-can-i-unpack-a-generator I guess it's just unfortunate that there's the extra set of parens. One of those links suggested the same solution as the first answer here. – Dubslow Jul 10 '12 at 21:41