def calculate_with_timeout(worksheet, usercode, timeout_seconds, private_key): it = InterruptableThread(target=calculate, args=(worksheet, usercode, private_key)) it.start() it.join(timeout_seconds) while it.isAlive(): it.interrupt() sleep(0.1)
def test_interruptable_thread_is(self): def sleep_lots(): # Setting stderr to None keeps test output from # having random stack traces inserted sys.stderr = None start = time.clock() while time.clock() - start < 10: pass self.fail('thread not interrupted') it = InterruptableThread(target=sleep_lots) it.start() it.join(5) self.assertTrue(it.isAlive()) it.interrupt() time.sleep(1) self.assertFalse(it.isAlive())