python出现typeerror原因是,为什么在Python的线程处理中出现TypeError

I've got the following code which is based off an example i found here on SO, but when i run it i get an error. Please help, i'm sure its very simple:

def listener(port):

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.bind(('',port))

sock.settimeout(1) # n second(s) timeout

try:

while True:

data, addr = sock.recvfrom(1024)

print data

except socket.timeout:

print 'Finished'

def startListenerThread(port):

threading.Thread(target=listener, args=(port)).start()

The error i get is:

Exception in thread Thread-1:

Traceback (most recent call last):

File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner

self.run()

File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 477, in run

self.__target(*self.__args, **self.__kwargs)

TypeError: listener() argument after * must be a sequence, not int

解决方案

The error is coming from the following line:

threading.Thread(target=listener, args=(port)).start()

The args parameter needs to be a sequence, I think your intention is to use a tuple, but wrapping a single value in parentheses does not accomplish this. Here is what you need to change it to:

threading.Thread(target=listener, args=(port,)).start()

Here is a simple example showing the difference:

>>> (100) # this is just value 100

100

>>> (100,) # this is a tuple containing the value 100

(100,)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值