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,
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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.