def first_solution(func): global next_answer co = _stackless.clonable() co.bind(func) try: co.switch() except Success, e: return e.args[0]
def first_solution(func): global next_answer co = _stackless.clonable() co.bind(func) pending = [(co, None)] while pending: co, next_answer = pending.pop() try: co.switch() except Fail: pass except Success, e: return e.args[0] else: # zero_or_one() called, clone the coroutine co2 = co.clone() pending.append((co2, 1)) pending.append((co, 0))
def test_clone_before_start(self): """Tests that a clonable coroutine can be cloned before it is started (this used to fail with a segmentation fault) """ import _stackless counter = [0] def simple_coro(): print "hello" counter[0] += 1 s = _stackless.clonable() s.bind(simple_coro) t = s.clone() s.switch() t.switch() assert counter[0] == 2
def test_myself_may_not_be_me_any_more(self): import gc from _stackless import clonable counter = [0] def runner(): while 1: assert clonable.getcurrent() is coro counter[0] += 1 main.switch() main = clonable.getcurrent() coro = clonable() coro.bind(runner) coro.switch() assert counter == [1] assert clonable.getcurrent() is main coro1 = coro.clone() assert counter == [1] assert clonable.getcurrent() is main coro.switch() assert counter == [2] coro.switch() assert counter == [3] assert clonable.getcurrent() is main del coro1 gc.collect() #print "collected!" assert clonable.getcurrent() is main assert counter == [3] coro.switch() assert clonable.getcurrent() is main assert counter == [4]