Created
December 15, 2019 11:36
-
-
Save 45deg/d022c3775c136262cb3bded30dd45002 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
import os | |
from multiprocessing import Pipe | |
class Continuation: | |
def __init__(self, cont): | |
self._cont = cont | |
def __call__(self, arg): | |
self._cont.send(arg) | |
exit(0) | |
def call_cc(f): | |
rx, tx = Pipe() | |
pid = os.fork() # 🍴 | |
if pid: | |
# 👨 | |
return f(Continuation(tx)) | |
else: | |
# 👶 | |
return rx.recv() | |
global_k = None | |
def f(k): | |
global global_k | |
print("f called") | |
global_k = k | |
return 0 | |
if __name__ == "__main__": | |
print("f returns", call_cc(f)) | |
if global_k: | |
global_k(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment