Search code examples
pythonmultithreadingpython-multithreading

pass function from class without starting it python


I need to run two functions with the threading module

you're able to do this Thread(target=my_func)

but i want the function from a class (imported class) so i tried Thread(target=a_class.my_func)

but it didn't worked, then i tried Thread(target=a_class.my_func())

but this one starts to run the function because i called it , and this is an infinite loop so the next function would never run.

how do i supposed to do it?


Solution

  • You should wrap the class methods, like this:

    import threading
    
    def my_func(self):
        a_class.my_func()
    
    def my_other_func(self):
        a_class.my_other_func()
    
    t1 = threading.Thread(target=my_func)
    t2 = threading.Thread(target=my_other_func)