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...

2 Answers 2

1

You need to make a new tuple.

t = Thread(target = do_something, args = arguments + (the_queue, )) 
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, thank you very much. I didn't know you could concatenate a tuple to another one that way. Cheers!
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() 

2 Comments

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"
I have no idea. I use Python 3.2. A quick Google turned up some similar problems here. stackoverflow.com/questions/3870778/… stackoverflow.com/questions/10967819/… 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.