17

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
Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38
Chris
  • 39,719
  • 45
  • 189
  • 235
  • 2
    The title of this question should be a real question like "why do I get "argument after * must be a sequence) when creating a ThreadQ?" . Proper question names help people to find the right question/answers – RubenLaguna May 27 '15 at 11:20

4 Answers4

51

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,)
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
8

In the last line, args=(port) is equivalent to args=port. You need to put port into a proper tuple like this: args=(port,).

senderle
  • 145,869
  • 36
  • 209
  • 233
6

The args parameter to threading.Thread needs to be a tuple

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

Note that it's not the parens that makes something a 1-element tuple, it's the comma:

>>> type((1))
<type 'int'>
>>> type((1,))
<type 'tuple'>

change the above to

threading.Thread(target=listener, args=(port,)).start()
bgporter
  • 35,114
  • 8
  • 59
  • 65
3

Found the answer here: Python Threading String Arguments

Rookie mistake: (port) doesn't make a tuple, i need either args=(port,) or args=[port].

Community
  • 1
  • 1
Chris
  • 39,719
  • 45
  • 189
  • 235