简体   繁体   中英

Passing arguments to a thread in python

I keep getting the error

Traceback (most recent call last):
File "main.py", line 37, in <module>
controller = threading.Thread(target=get_controller(), args=(q,))
TypeError: get_controller() missing 1 required positional argument: 'q'

Whenever I try to create a thread in python. Here's the code I'm using

def get_controller(q):
    q.put(get_gamepad())
return

q = queue.Queue()
events = []
controller = threading.Thread(target=get_controller(), args=(q,))

I know it's not the common error of forgetting to add a "," after the first argument in "args", but I'm not sure what else it could be.

When you pass the target, you should pass the pointer to the function you want. Instead, what you are doing is calling the function, which is not your desired effect. Try this instead:

controller = threading.Thread(target=get_controller, args=(q,))

You should not use () in target=

threading.Thread(target=get_controller, args=(q,))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM