1

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!

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • [Add](http://stackoverflow.com/posts/27775810/edit) the complete exception traceback to your question. Also, add which Python version you're using. –  Jan 05 '15 at 08:37
  • John gives a better solution below. As to why your error occurs: `interruptingcow` is at fault for using an undocumented class that is private to the module. See [this issue](http://bugs.python.org/issue10859), which shows `GeneratorContextManager` has been [renamed ](https://hg.python.org/cpython/file/f374e4e6d04b/Lib/contextlib.py#l34). –  Jan 05 '15 at 09:02
  • So i also tried to rename the import in interuptingcow from from contextlib import GeneratorContextManager to from contextlib import _GeneratorContextManager and just to be safe i evain tried: from contextlib import __GeneratorContextManager cause i thought that is what you are sugesting with [renamed] post... non of which work – Smrdo Smrdi Zajc Jan 05 '15 at 10:08
  • No no no: don't try and fix a broken module, particularly not by using undocumented private classes. Get rid of that cow module and use something else. –  Jan 05 '15 at 10:17
  • Ok, thank you for the answer :D The problem is this is the only thing i found until now that can do this on windows – Smrdo Smrdi Zajc Jan 05 '15 at 10:25
  • Perhaps an [interesting read](http://eli.thegreenplace.net/2011/08/22/how-not-to-set-a-timeout-on-a-computation-in-python). –  Jan 05 '15 at 10:31

1 Answers1

0

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.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Hey thank you for the quick response! I probably should have said earlier i am using windows :D so this is not a viable solution. SIGALRM does not exist on windows and signal.alarm is also avaliable on unix not windows. Sorry if i messed something up. – Smrdo Smrdi Zajc Jan 05 '15 at 09:59
  • But this module uses SIGALRM, perhaps that’s the issue then? – David Kong Oct 23 '20 at 05:03