Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python threading app error to many arguments

What is wrong with this python source code?

import threading
import subprocess as sub

def ben(fil):
    pr = sub.Popen(fil,stdout=sub.PIPE,stderr=sub.PIPE)
    output, errors = pr.communicate()
    print output

theapp = '''blender
            blender-softwaregl'''.split()
print theapp

for u in theapp:
    print u
    tr = threading.Thread(target=ben, args=(u))
    tr.daemon = True
    tr.start()

The error is :

['blender', 'blender-softwaregl']
blender
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: ben() takes exactly 1 argument (7 given)

blender-softwaregl
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: ben() takes exactly 1 argument (18 given)

This is my problem.What is this error?

 TypeError: ben() takes exactly 1 argument (18 given)
like image 786
Cătălin George Feștilă Avatar asked Dec 18 '12 12:12

Cătălin George Feștilă


1 Answers

The args argument to threading.Thread expects a sequence, but you're providing a string. This is causing it to interpret each letter of the strings as an individual argument, resulting in too many arguments for your target function.

You're very close to having the right code. You just need to fix your tuple syntax by adding a trailing comma in the parenthases:

tr = threading.Thread(target=ben, args=(u,)) # comma makes args into a 1-tuple
like image 65
Blckknght Avatar answered Nov 07 '22 22:11

Blckknght