Python : Running function in thread does not modify current_thread()

后端 未结 2 1200
天命终不由人
天命终不由人 2020-12-16 23:27

I\'m currently trying to figure out how threads work in python.

I have the following code:

def func1(arg1, arg2):

    print current_thread()
    ...         


        
相关标签:
2条回答
  • 2020-12-16 23:34

    You are calling the function before it is given to the Thread constructor. Also, you are giving it as the wrong argument (the first positional argument to the Thread constructor is the group). Assuming func1 returns None what you are doing is equivalent to calling threading.Thread(None) or threading.Thread(). This is explained in more detail in the threading docs.

    To make your code work try this:

    t1 = threading.Thread(target=func1, args=(arg1, arg2))
    t1.start()
    t1.join()
    
    0 讨论(0)
  • 2020-12-16 23:41

    You're executing the function instead of passing it. Try this instead:

    t1 = threading.Thread(target = func1, args = (arg1, arg2))
    
    0 讨论(0)
提交回复
热议问题