Search code examples
pythonpython-3.xmultithreadingpython-multithreading

Can't create thread because "Takes 1 positional argument but 4 given" only when I give it a string longer than 1


I am making a chat server in python. I have been creating threads fine so far, but when I try to create one, with the argument of a username, it fails with the above error - but only when the username is more than 1 character.

If I give it the username "A", it works fine, but the username "Alex" gives the error. How do I fix this?

They are in the same class.

I create the thread with

Thread(target=Main.ManageClientHighLevel, args=(Username)).start()

And the start of that function is:

def ManageClientHighLevel(Username):

How do I fix this?


Solution

  • Thread's args argument expects an Iterable, so you will have to provide your single argument in a tuple:

    Thread(target=Main.ManageClientHighLevel, args=(Username,)).start()
    

    Otherwise, it will handle your single provided string as an Iterable and iterate over each character.