0

I create a main function below with two argument function inside Thread.

if __name__ == "__main__":
    # Define ports
    ports_for_server_connection=[10003, 10004, 10005, 10006]
    for port_number in ports_for_server_connection:
        # Open multi thread sockets so that each will respond independently
        t = Thread(target=openServer, args=(port_number, 1))
        t.start()

However, I want to create that function with only one argument. When I tried to implement it with one argument(args=(port_number, 1)) I got the following error.

Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
TypeError: openServer() argument after * must be an iterable, not int

How can I use thread with only one argument function?

Thanks,

2
  • show openServer function definition Commented Sep 2, 2019 at 17:40
  • 'def openServer(port, listen_on):' Commented Sep 2, 2019 at 17:44

1 Answer 1

1

Pass it only one argument:

t = Thread(target=openServer, args=(port_number,))

The trick bit is that (x,) is a tuple of length one those first item is x. If this is too hard just use a list:

t = Thread(target=openServer, args=[port_number])
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried what you suggest and it works well. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.