Skip to content

Instantly share code, notes, and snippets.

@exhuma
Created September 26, 2016 06:01
Show Gist options
  • Save exhuma/429602945094152a4632aa0bd767b902 to your computer and use it in GitHub Desktop.
Save exhuma/429602945094152a4632aa0bd767b902 to your computer and use it in GitHub Desktop.
Example of running functions at given time intervals.
from time import sleep
import logging
LOG = logging.getLogger(__name__)
def every_5_seconds():
LOG.info('Running every 5')
def every_3_seconds():
LOG.info('Running every 3')
def every_1_minutes():
LOG.info('Running every minute')
def main():
elapsed_seconds = 0
while True:
LOG.info('tick #%d', elapsed_seconds)
if elapsed_seconds % 5 == 0:
every_5_seconds()
if elapsed_seconds % 3 == 0:
every_3_seconds()
if elapsed_seconds % 60 == 0:
every_1_minutes()
sleep(1)
elapsed_seconds += 1
if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s | %(levelname)s | %(message)s',
level=logging.DEBUG
)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment