0

Probably I am overlooking something very basic. I have a function

def execution(command):
    os.system(command)

And another function

def start_this_thread():
    server_thread = threading.Thread(target=execution, args=(exec_str))
    server_thread.start()

I get an error:

self.run()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 483, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: execution() takes exactly 1 argument (233 given)

Aparently the string length (command) is of length 233??

3 Answers 3

2

Ok.. I figured it out..

Instead of

  server_thread = threading.Thread(target=execution, args=(exec_str))

it should be

 server_thread = threading.Thread(target=execution, args=(exec_str,))

Though Would love to know why?

Sign up to request clarification or add additional context in comments.

1 Comment

Because when you want to define tuple with only 1 argument you need to write (blabla, ) not (blabla). And when you run threading.Thread(target=execution, args=(exec_str)) and, for instance, exec_str == '123' args will equal to ['1', '2', '3'] not ['123']
1

args is interpreted simply as a sequence of arguments. You passed in (args_str), which is a string (since the pair of brackets is interpreted simply as grouping, not as a tuple constructor). So, the string is expanded as a sequence into 233 separate arguments (one for each character in the string).

Use (args_str,) instead (note trailing comma) to create a one-element tuple.

Comments

0

Your problem is that args gets expanded, which afaik means that exec_string is going from 1 item into 233. Try putting a comma after exec_string to make it a literal tuple instead of parentheses. I'm on mobile right now but will edit tomorrow sometime for formatting and clarity.

In python, (something)==something, but (something,) is a tuple of 1 item with something as the only element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.