Home > Enterprise >  Make 2 functions run at the same time in loop
Make 2 functions run at the same time in loop

Time:07-31

I am trying to make 2 functions run at the same time in while loop.

    import time


def func1():
    print('1')
    time.sleep(2)


def func2():
    print('2')


if __name__ == '__main__':
    while True:
        func1()
        func2()

Thanks in advance!

CodePudding user response:

This can be done by running the two functions in separate threads.

import time, threading

def func1():
    print('1')
    time.sleep(2)

def func2():
    print('2')

if __name__ == "__main__":
    while True:
        threading.Thread(target=func1).start()
        threading.Thread(target=func2).start()
  • Related