Loop interrupt with an import module "interrupringcow" import error
Question: I have a problem with a downloaded module named interruptingcow It is supposed
to allow me to interrupt a loop after a set amount of time. I have the latest
version that i found installed with pip.

    
    
    import time
    from random import *
    from interruptingcow import timeout
    points = 0
    error = 0
    
    print('Today we will learn calculus! We will multiply random numbers from 2-10.\nPress Enter when ready!')
    input()
    
    try:
        with timeout(20, exception=RuntimeException):
            while True:
                XX, YY = randint(2, 10), randint(2, 10)
                print(XX, 'X', YY)
                if XX*YY == int(input('odgovor? > ')):
                    print('GG! +1 point')
                    points += 1
                else:
                    print('Error! The real result is:', XX*YY)
                    error = 1
                    break
    except RuntimeException:
        print('Your time is UP!')
    
    print('Your score is:',points,'Thanks for playing! Press enter when you want to finish!')
    input()
    

I think the problem is with the version of python? but i dont know how to fix
it. Any ideas? RuntimeException is underlined in the program, that is probably
one of the errors, the other one is this: ImportError: cannot import name
'GeneratorContextManager'

I looked at interruptingcow code and one of the imports is:

    
    
    from contextlib import GeneratorContextManager
    

Thank you guyz in advance!


Answer: You don't need this `interruptingcow` module at all, it's easy to set up a
single timer to interrupt your loop with Python's built-in `signal` module:

    
    
    import signal
    
    def handler(signo, frame):
        raise RuntimeError
    
    signal.signal(signal.SIGALRM, handler)
    signal.alarm(1) # seconds
    
    while True:
        print 'zzz'
    

Just substitute your own `while True` loop and I think this will do what you
need, without the extra dependency.

