Python:TypeError:*之后的参数必须是序列

时间:2016-04-03 15:51:17

标签: python multithreading typeerror

我有这段代码,我尝试在新线程中发送UDP数据报

import threading, socket

address = ("localhost", 9999)


def send(sock):
    sock.sendto("Message", address)
    print "sent"

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
threading.Thread(target=send, args=(s)).start()

但是当我尝试将套接字作为函数的参数时,会抛出一个TypeError异常:

Exception in thread Thread-1:
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 
    self.__target(*self.__args, **self.__kwargs)
TypeError: send() argument after * must be a sequence, not _socketobject

这意味着什么?

1 个答案:

答案 0 :(得分:21)

您需要在变量,之后添加逗号 - s。只发送s到args =()试图解包一些参数,而不是只发送那个争论。

所以你有threading.Thread(target=send, args=(s,)).start()

另外,splat - * - 运算符可能会在this问题中解释它的用法和解压缩参数