Search code examples
pythonclasspython-multithreading

How to call class function from seperate class function through Threading module Python


I'm coding my own Asteroids minigame using pygame (which is extremely inefficient with multiple sprites so wanted to use threading.

How do I access a function from another class (asteroids class) through my main class (handler) class.

If my class system is backwards or doesn't work using Threading, please explain to me how I could make it more efficient or easy to code in

class Asteroids():
    def __init__(self):
    ....
    def Update(self):
    ....

class Handler():
    def __init__(self):
    ....
    def Update_All(self):
    x = threading.Thread(target = ##Asteroids.Update()
    x.start()

thanks in advance


Solution

  • You need to create an instance of Asteroids

    astroid = Astroids()
    # then you can use the function in your thread
    x = threading.Thread(target = astroid.Update)