11

I have this piece of code in which I try to send an UDP datagram in a new thread

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() 

But when I try to give the socket as an argument to the function, a TypeError exception is thrown:

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 

What that means?

1 Answer 1

31

You need to add a comma - , - after your variable s. Sending just s to args=() is trying to unpack a number of arguments instead of sending just that single arguement.

So you'd have threading.Thread(target=send, args=(s,)).start()

Also the splat - * - operator might be useful in this question explaining it's usage and unzipping arguments in general

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.