Python线程超过一个agrument给出

时间:2015-09-24 12:28:01

标签: python multithreading arguments

我正在尝试启动线程并且我不断收到并且错误消息说我正在尝试发送多个参数。似乎Thread对象不将变量port作为一个参数,而是将字符串中的每个字符作为一个单独的参数。这是怎么回事?这是我第一次在python中进行多线程处理。

错误讯息:

Exception in thread /dev/ttyUSB0:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: report() takes exactly 1 argument (12 given)

代码:

def report(port):
    print("\n")
    print(st() +"Connecting to" + port[0]) 

    device = serial.Serial(port=port[0], baudrate=9600, timeout=0.2)

    print (st() + "Connection sucessfull...")       
    print (st() + "Initializing router on "+ port[0])
    if initialize_router(device) == 0:
        return 0

    print (st() + "Initialization sucessfull")
    print (st() + "Starting to inject IP basic config")
    if inject_config(device) == 0:
        print(errror("injecing the confing",port[0]))
        return 0

    print(st()+ "Finished injecting default IP setting on router connected to " + port[0])

    return 1

if __name__ == '__main__':  
    ports = list_ports.comports()
    list_port = list(ports)
    port_counter = -1
    for port in list_port:
        if "USB" in port[0]:
            port_counter = port_counter + 1
            port = "/dev/ttyUSB" + str(port_counter)
            thread = Thread(target=report, args=(port), name=port)
            thread.start()
            print port
            print ("\n")
            continue

1 个答案:

答案 0 :(得分:4)

        thread = Thread(target=report, args=(port), name=port)

我猜你想在这里将单个元素元组传递给args。但是围绕port的那些括号本身没有任何影响。尝试:

        thread = Thread(target=report, args=(port,), name=port)