Python 线程 - 参数数量错误

6 投票
1 回答
3085 浏览
提问于 2025-04-17 02:08

我在一个线程里执行一个命令,差不多执行了25000次,

if threaded is True:
                thread = Thread(target=threadedCommand, args=(cmd))
                thread.start()
                thread.join()  

def threadedCommand(command):
    if command is None:
        print 'can\'t execute threaded command'
        sys.exit(-1)
    print 'executing - %s'%(command)
    os.system(command)  

这个命令大概是这样的:

cp file dir

然后我看到的错误信息是:

追踪记录(最近的调用在最前面):文件 "/usr/lib64/python2.6/threading.py",第525行,在 __bootstrap_inner 中 self.run() 文件 "/usr/lib64/python2.6/threading.py",第477行, 在 run 中 self.__target(*self.__args, **self.__kwargs) 类型错误: threadedCommand() 需要一个参数(给了52个)

^C线程 Thread-9377 中出现异常:追踪记录(最近的调用在最前面): 文件 "/usr/lib64/python2.6/threading.py",第525行,在 __bootstrap_inner 中 self.run() 文件 "/usr/lib64/python2.6/threading.py",第477行, 在 run 中 self.__target(*self.__args, **self.__kwargs) 类型错误: threadedCommand() 需要一个参数(给了56个)

1 个回答

22

args 必须是一个元组。(cmd)cmd 是一样的;你需要的是一个只有一个元素的元组:

thread = Thread(target=threadedCommand, args=(cmd,))
#                                                ^

撰写回答