Created
July 24, 2020 07:43
-
-
Save JustusAdam/609adc23e81636c8edcf8eaaaadbc9ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ref: | |
sentinel = object() | |
def __init__(self): | |
self._val = self.sentinel | |
def set(self, val): | |
self._val = val | |
def get(self): | |
t = self._val | |
self._val = self.sentinel | |
return t | |
def interruptible(ref, gen): | |
for i in gen: | |
x = ref.get() | |
if x is not Ref.sentinel: | |
yield x | |
yield i | |
global_ref = Ref() | |
def foo(): | |
for res in interruptible(global_ref, bar()): | |
print(res) | |
return 0 | |
def bar(): | |
yield 4 | |
yield 3 | |
baz() | |
yield 1 | |
def baz(): | |
# somehow force bar to `yield` | |
# or `return` 2 | |
global_ref.set(2) | |
print(foo()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment