1

I'm working on an irl minigame where you get materials every 5 minutes. To monitor this i wanted to write a simple python script. But now there is a little roadblok,

how do you make a loop that does something every x minutes, while still running other keyboard inputs without it disrupting the loop?

M. Storms
  • 11
  • 2
  • 3
    Provide your source code (or at least the relevant parts) – Fabien Jul 16 '17 at 16:28
  • 1
    First read this learn how to ask a question from following link https://stackoverflow.com/help/asking – Kallz Jul 16 '17 at 16:28
  • 1
    We're not supposed to offer tutorials here on SO but let me bend the rules. You probably need to read about organising your code into *threads*. One of these threads would wake up every five minutes and do something. The other thread would appear to run continuously to handle keyboard and mouse events. – Bill Bell Jul 16 '17 at 16:59

2 Answers2

3

Here's a fairly simple example of using a threading.Timer. It displays the current time every 5 seconds while responding to user input.

This code will run in any terminal that supports ANSI / VT100 Terminal Control Escape Sequences.

#!/usr/bin/env python3

''' Scrolling Timer

    Use a threading Timer loop to display the current time
    while processing user input

    See https://stackoverflow.com/q/45130837/4014959

    Written by PM 2Ring 2017.07.18
'''

import readline
from time import ctime
from threading import Timer

# Some ANSI/VT100 Terminal Control Escape Sequences
CSI = '\x1b['
CLEAR = CSI + '2J'
CLEAR_LINE = CSI + '2K'
SAVE_CURSOR = CSI + 's'
UNSAVE_CURSOR = CSI + 'u'
GOTO_LINE = CSI + '%d;0H'

def emit(*args):
    print(*args, sep='', end='', flush=True)

# Show the current time in the top line using a Timer thread loop
def show_time(interval):
    global timer
    emit(SAVE_CURSOR, GOTO_LINE % 1, CLEAR_LINE, ctime(), UNSAVE_CURSOR)
    timer = Timer(interval, show_time, (interval,))
    timer.start()

# Set up scrolling, leaving the top line fixed
emit(CLEAR, CSI + '2;r', GOTO_LINE % 2)

# Start the timer loop
show_time(interval=5)

try:
    while True:
        # Get user input and print it in upper case
        print(input('> ').upper())
except KeyboardInterrupt:
    timer.cancel()
    # Cancel scrolling
    emit('\n', SAVE_CURSOR, CSI + '0;0r', UNSAVE_CURSOR)

You need to send a KeyboardInterrupt, that is, hit CtrlC to stop this program,

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
0

Maybe a timer will be helpful for your task. I recommend you to check this link: https://docs.python.org/2.4/lib/timer-objects.html. While the timer is counting you are able to do other tasks and when the time is up, you can attach a function to the timer to do something. Timers from this library inherits from Threads

JBeloqui
  • 27
  • 5
  • 1
    Is there some particular reason that you're linking to ancient Python 2.4 docs? The latest version is [here](https://docs.python.org/3/library/threading.html#timer-objects). – PM 2Ring Jul 16 '17 at 17:04
  • Was my mistake, I did not realize that it was for Python 2.x; I will take extra care for the next time which version I am linking to. However the idea is the same, I believe it is a good solution for the problem – JBeloqui Jul 17 '17 at 23:25