Python线程 - 参数数量错误

时间:2021-12-23 04:58:27

I am executing a command in a thread for almost 25k times like

我在一个线程中执行命令几乎是25k次

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)  

and command is like

和命令就像

cp file dir

and what I see is

而我所看到的是

Traceback (most recent call last): File "/usr/lib64/python2.6/threading.py", line 525, in __bootstrap_inner self.run() File "/usr/lib64/python2.6/threading.py", line 477, in run self.__target(*self.__args, **self.__kwargs) TypeError: threadedCommand() takes exactly 1 argument (52 given)

回溯(最近一次调用最后一次):文件“/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)TypeError:threadedCommand()只取1个参数(给定52个)

^CException in thread Thread-9377: Traceback (most recent call last): File "/usr/lib64/python2.6/threading.py", line 525, in __bootstrap_inner self.run() File "/usr/lib64/python2.6/threading.py", line 477, in run self.__target(*self.__args, **self.__kwargs) TypeError: threadedCommand() takes exactly 1 argument (56 given)

线程中的CException Thread-9377:Traceback(最近一次调用最后一次):文件“/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)TypeError:threadedCommand()只取1个参数(给定56个)

1 个解决方案

#1


20  

args must be a tuple. (cmd) is the same as cmd; you want a one-element tuple instead:

args必须是一个元组。 (cmd)与cmd相同;你想要一个单元素元组:

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

#1


20  

args must be a tuple. (cmd) is the same as cmd; you want a one-element tuple instead:

args必须是一个元组。 (cmd)与cmd相同;你想要一个单元素元组:

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