python线程应用错误到许多参数 [英] python threading app error to many arguments
本文介绍了python线程应用错误到许多参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此python源代码有什么问题?
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()
错误是:
['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)
推荐答案
threading.Thread
的args
参数需要一个序列,但是您要提供一个字符串.这导致它将字符串的每个字母解释为一个单独的参数,从而导致目标函数的参数过多.
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
这篇关于python线程应用错误到许多参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文