I am kind of new to python and have recently been working on writing a python script to automate a game via emulators and for that I have been using threading. Now I wanted to make some changes to a function and suddenly I get an error message from the thread that 13 arguments were given instead of the required 1.
The original code goes along the following lines:
def go_to_main_coords(device, main_coords):
click(device, (9, 908))
click(device, (9, 908))
go_to_coords(device, main_coords)
main_thread = threading.Thread(target=go_to_coords, args=(main_device, support_coords))
support_thread = threading.Thread(target=go_to_main_coords, args=(support_device, main_coords))
#Start threads
main_thread.start()
support_thread.start()
#going to coords
main_thread.join()
This worked perfectly fine. The issue started appearing after I changed the go_to_main_coords() function to speed up this step which gets called quite a few times.
After the changes the relevant code looked as follows:
def go_to_main_coords(device):
click(device, (9, 908))
click(device, (9, 908))
click(device, (40, 820))
click(device, (260, 800))
click(device, (270, 400))
main_thread = threading.Thread(target=go_to_coords, args=(main_device, support_coords))
support_thread = threading.Thread(target=go_to_main_coords, args=(support_device))
# Start threads
main_thread.start()
support_thread.start()
# going to coords
main_thread.join()
When running the version of the script I now get the following message once the code arrives at this part:
TypeError: go_to_main_coords() takes 1 positional argument but 13 were given
Why I think this issue has to do with the string itself and the processing of it is because the variable support_device passed on to the function go_to_main_coords() is a string such as "emulator-5564" which fits the exact number of "arguments" that are apparently given. If the program continues to run the support_device eventually changes to something along the lines of "localhost:5595" and the error message then says 14 arguments were given which again lines up with the number of characters in the string.
My question now is, why does it work when giving the function 2 arguments but not with just one. I even tried running it where go_to_main_coords() requires 2 arguments but the second one is just ignored. That works as well. Yet once I remove the second argument from both the call as well as the definition it keeps giving me this error.
support_thread = threading.Thread(target=go_to_main_coords, args=(support_device))
The args
is supposed to be a tuple of arguments. Since there is only one argument now, the parentheses are not interpreted as a tuple but just as precedence override, like in (1 + 2) * 3
. Add a trailing comma to fix this:
support_thread = threading.Thread(target=go_to_main_coords, args=(support_device,))
See e.g. TupleSyntax on the Python wiki.