frazman
frazman

Reputation: 33243

threading error in python

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

Upvotes: 0

Views: 112

Answers (3)

nneonneo
nneonneo

Reputation: 179422

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.

Upvotes: 1

AI Generated Response
AI Generated Response

Reputation: 8835

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.

Upvotes: 0

frazman
frazman

Reputation: 33243

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?

Upvotes: 2

Related Questions