4 import time
, thread
, weakref
, gc
7 def __init__(self
, computation
, stopper
):
8 self
.container
= [None]
9 self
.computation
= computation
10 self
.stopper
= stopper
14 self
.container
[0] = self
.computation()
16 self
.container
[0] = repr(e
)
19 def __init__(self
, x
):
25 def spawn(computation
, stopper
):
26 proc
= Process(computation
, stopper
)
29 wref
= weakref
.ref(token
, lambda arg
: stopper())
37 if x
is None: return ('working', None)
38 else: return ('done', x
)
41 return '<Retriever holding value %s>' % str(self())
45 for kid
in self
.children
:
48 def spawn(self
, comp
, stopr
):
49 p
= globals().spawn(comp
, stopr
)
50 self
.children
.append(p
)
53 thread
.start_new_thread(proc
.run
, ())
62 raise Exception, "Cancelled"
66 return fib(n
- 1, go
) + fib(n
- 2, go
)
70 def stop(): go
[0] = False
71 return spawn(lambda: fib(n
, go
), stop
)