8

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)

2 Answers 2

21

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
Sign up to request clarification or add additional context in comments.

2 Comments

you could wrap it in a list Thread(target=ben, args=[u]) as an alternative. It might be easier to read though a tuple is more valid semantically.
If i use args=[u] is not working , also if I use args=(u,) seam working but: File "/usr/local/lib/python2.7/subprocess.py", line 1237, in _execute_child <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'close' So is need to close the thread ? Do you can help me ?
0

For some reason it seems that you are passing lists of characters as arguments instead of strings.

I got that cause blender has 7 letters and you got Type error 7 arguments passed. And the blender-softwaregl has 18 letters so you've got Type error for 18 arguments instead of one.

If you want to pass more than one arguments to the subprocess. Popen, try passing the dictionary with the variable name fill and the value list.

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

d = {'fil':['command1', 'command2']}

ben(**d)

One more thing. Why are you opening a Thread if you are opening the subprocess??? A subprocess is a separate process, you dont need the thread part. And your code will work without the thread part, I guess.

5 Comments

I don't understand. can you give more infos? You tell me about d and **d[args] . What is the problem ? Why is 18 args when i send just one? I'm a little lost in this. Thank you. regards.
The d is a variable holding a dictionary, when you put **d['args'] that means you are passing a dictionary as arguments, thats all... But i figured out your problem! Blender has 7 letters blender-softwaregl haz 18 letters I guess you are somehow passing a list of characters as arguments :)
I was fast to add my answer, so I will fix it. Anyway I was looking int your code and I noticed one more thing...
You dont need threading at all, just replace tr = threading.Thread(target=ben, args=(u)) tr.daemon = True tr.start() with ben(fil)
I want to use time command to see how the fast is open this binary. This is the reason for using threading. args=(u,) is the solution for.

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.