Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:TypeError:*后面的参数必须是序列_Python_Multithreading_Typeerror - Fatal编程技术网

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

Python:TypeError:*后面的参数必须是序列,python,multithreading,typeerror,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)).sta

我有一段代码,其中我尝试在新线程中发送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

这意味着什么?

您需要在变量
s
之后添加逗号-
。仅发送s到args=()是尝试解压大量参数,而不是仅发送单个参数

因此您需要
threading.Thread(target=send,args=(s,).start()

另外,splat-
*
-运算符在解释其用法和解压参数时可能会很有用