Skip to content

Instantly share code, notes, and snippets.

@JustusAdam
Created July 24, 2020 07:43
Show Gist options
  • Save JustusAdam/609adc23e81636c8edcf8eaaaadbc9ff to your computer and use it in GitHub Desktop.
Save JustusAdam/609adc23e81636c8edcf8eaaaadbc9ff to your computer and use it in GitHub Desktop.
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