Search code examples
pythonmultithreadingsimulation

Threading in python, how to do?


I'm working on a pseudo-life simulation, it works great so far but I need help on how to do something.

I'm going to paste my code, I explain the problem in a # TODO: comment.


        def speciesMaking(rng): # TODO: After a species is made, do AI(animal_instance) and make sure IT DOESN'T REPEAT!! The reason it doesn't work right now is because I don't know how threading works
            animal_instance.name = namingproc()
            rng2 = random.randint(0, 5)
            if rng == 0:
                animal_instance.isCarnivore=True
                animal_instance.hierarchy=random.randint(3, 10)
                animal_instance.size=random.randint(3, 10)
                print("Carnivore = True")
                
                if rng2 > 4:
                    print("Scavenger = True")
                    animal_instance.isScavenger=True
                    animal_instance.hierarchy=random.randint(3, 10)
                    animal_instance.size=random.randint(3, 10)
                    


            if rng == 1:
               animal_instance.isOmnivore=True    
               animal_instance.hierarchy=random.randint(3, 10)
               animal_instance.size=random.randint(3, 10)
               print("Omnivore = True")
               
               if rng2 > 4:
                   animal_instance.isScavenger=True
                   animal_instance.hierarchy=random.randint(3, 10)
                   animal_instance.size=random.randint(3, 10)
                   print("Scavenger = True")
                   
            
            

            if rng == 2:
                animal_instance.isHerbivore=True  
                animal_instance.hierarchy=random.randint(3, 10)
                animal_instance.size=random.randint(3, 10)
                print("Hervibore = True") 
                

            if rng == 3:
                rng = random.randint(0, 4)
                speciesMaking(rng)
                


            elif rng == 4:
                if rng2 == rng2:
                     animal_instance.isDecomposer=True
                     if random.randint(0, 2) == 1:
                         animal_instance.size=1
                         animal_instance.hierarchy=0
                         animal_instance.isFlora=1

                         print("Flora = True")
                        

                     elif 1 == 1:    
                        
                         
                         animal_instance.hierarchy=0
                         animal_instance.size=0
                         animal_instance.isBacteria=True
                         animal_instance.isDecomposer=True
                         print("Bacteria = True")
                         
                        
                     
                     print("Decomposer = True")
                     

                else:
                    rng = random.randint(0, 4)
                    rng2 = random.randint(0, 5)
                    speciesMaking(rng)

                
            print(animal_instance.name, " = ", animal_instance)   

Basically, i want to make so that after every animal_instance is created, their AI AI(animal_instance) is done, but the function is an infinite loop, i need to multitask so that MULTIPLE function calls go to different threads so the species can be created as the AI is working


Solution

  • You haven't given us the full code so I cannot actually show you the solution, but I can explain how threads are used in python.

    animal_thread = threading.Thread(target=self.run_AI, args=('your args here'))    #create a new thread object that will run the 'target' function with whatever args are defined in 'args'
    animal_thread.daemon = True    #this makes the thread close when you exit your program
    animal_thread.start()    #actually starts the thread
    

    I have put in placeholder names where you would want to insert the actual function and args, but you get the idea. This code would normally go in your init function, which would make the thread start whenever an instance is created. Feel free to let me know if you have any other questions!