'How to run 2 loops side by side in python?

I have tried both of these solutions

from multiprocessing import Process

    def loop_a():
    
        while 1:
    
            print("a")
    
    def loop_b():
    
        while 1:
    
            print("b")
    
    if __name__ == '__main__':
    
        Process(target=loop_a).start()
    
        Process(target=loop_b).start()

and the second solution

import threading
import time

def infiniteloop1():
    while True:
        print('Loop 1')
        time.sleep(1)

def infiniteloop2():
    while True:
        print('Loop 2')
        time.sleep(1)

thread1 = threading.Thread(target=infiniteloop1)
thread1.start()

thread2 = threading.Thread(target=infiniteloop2)
thread2.start()

but both of these dont work whichever loop is above like in the second case infiniteloop1 is above so infiniteloop1 will run then after it ends infiniteloop2 will run and i want BOTH of them to run at the same time what i think that the problem is that both the loops will take input from user in some kind like in the first loop it will take voice input and in the second loop it will see if a mouse button is pressed. so while one loop is taking input the other wont run something like that is happening from what i think this is the code of my loops loop 1

def screen_display():
    white = (255, 255, 255)
    screen = pygame.display.set_mode((320, 600))
    pygame.display.set_caption("Assistant")
    screen.fill(white)
    exit_button = pygame.image.load('cross.png').convert_alpha()
    set_height = exit_button.get_height()
    set_width = exit_button.get_width()
    exit_button_rect = exit_button.get_rect(center=(230, 545))
    settings_button = pygame.image.load('settings.png').convert_alpha()
    pygame.transform.scale(settings_button, (set_height, set_width))
    settings_button_rect = settings_button.get_rect(center=(67, 545))
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            mousepos = pygame.mouse.get_pos()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if is_over(settings_button_rect, mousepos):
                        print(1)
                    if is_over(exit_button_rect, mousepos):
                        print(2)

loop 2

while True:

        # Exception handling to handle
        # exceptions at the runtime

        try:

            # use the microphone as source for input.
            global source2
            with sr.Microphone() as source2:

                # wait for a second to let the recognizer
                # adjust the energy threshold based on
                # the surrounding noise level
                settings()
                print("say the command after the beep")
                SpeakText("What do you want me to do?")
                MyText = speech_text()
                turn_text_to_sense(MyText)
                if 'your name' in MyText:
                    print("You can change my name say 'help' for help")

        except sr.RequestError as e:
            SpeakText("Could not request results please check your internet connection; {0}".format(e))

        except sr.UnknownValueError:
            SpeakText("Couldn't understand what you said, say 'help' for info about commands")

there is some more code of other functions but i dont think that they are important



Solution 1:[1]

You'd better use threading

import threading 

def loop_a():

    while 1:

        print("a")

def loop_b():

    while 1:

        print("b")

if __name__ == '__main__':
    threadings = []

    threadings.append(threading.Thread(target = loop_a))
    threadings.append(threading.Thread(target = loop_b))

    for thread_item in threadings:
        thread_item.start()

    for thread_item in threadings:
        thread_item.join()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1