user11834943
user11834943

Reputation:

Python Network Threading

Okay i want to program a server with python. Therefore i opened a socket and wait for the input. When i get an input i get an further socket and and address.

Because I want to implement multiple connections at the same time I looked into multi-threading in python.

I create a thread the following way:

t = Thread(target=input, args=(conn, address, ))
t.start()

My input method looks the following:

def input(conn, address): [...]

Now if in this way I get the following stacktrace:

Exception in thread Thread-1:

Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run()

File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, **self._kwargs)

TypeError: input expected at most 1 argument, got 2

If i remove the address (and just give the conn) it prints the following:

<socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('10.0.0.9', 3306), raddr=('10.0.0.9', 32908)>

How should I understand that?

Solution to the problem: I need to call the method via self.input not input.

Upvotes: 1

Views: 6788

Answers (1)

Vahid Kharazi
Vahid Kharazi

Reputation: 6033

input is a build-in python function which gets one argument so when you are trying to use input inside threading python supposed you want to use the built-in input function. change the function name will fix the issue:

def _input(conn, address): [...]


t = Thread(target=_input, args=(conn, address, ))
t.start()

PS: this way is not a good way to handle multithread networking

Upvotes: 1

Related Questions