comparison doc/v2_planning/plugin_greenlet.py @ 1196:e9bb3340a870

plugin_greenlet draft0
author James Bergstra <bergstrj@iro.umontreal.ca>
date Sun, 19 Sep 2010 13:06:16 -0400
parents d3ee0d2d03e6
children a60b3472c4ba
comparison
equal deleted inserted replaced
1195:d3ee0d2d03e6 1196:e9bb3340a870
36 36
37 def vm_loop(gr, dest, a, kw): 37 def vm_loop(gr, dest, a, kw):
38 while True: 38 while True:
39 if gr == 'return': 39 if gr == 'return':
40 return a, kw 40 return a, kw
41 print 'vm_loop gr=',gr,'args=',a, 'kwargs=', kw 41 #print 'vm_loop gr=',gr,'args=',a, 'kwargs=', kw
42 gr, dest, a, kw = gr.switch(vm, gr, dest, a, kw) 42 gr, dest, a, kw = gr.switch(vm, gr, dest, a, kw)
43 #print 'gmain incoming', incoming 43 #print 'gmain incoming', incoming
44 vm = greenlet(vm_loop) 44 vm = greenlet(vm_loop)
45 45
46 return vm.switch(prog, 'return', args, kwargs) 46 return vm.switch(prog, 'return', args, kwargs)
52 def repeat(N, glets): 52 def repeat(N, glets):
53 def repeat_task(vm, gself, dest, args, kwargs): 53 def repeat_task(vm, gself, dest, args, kwargs):
54 while True: 54 while True:
55 for i in xrange(N): 55 for i in xrange(N):
56 for glet in glets: 56 for glet in glets:
57 print 'repeat_task_i dest=%(dest)s args=%(args)s, kw=%(kwargs)s'%locals() 57 #print 'repeat_task_i dest=%(dest)s args=%(args)s, kw=%(kwargs)s'%locals()
58 # jump to task `glet` 58 # jump to task `glet`
59 # with instructions to report results back to this loop `g` 59 # with instructions to report results back to this loop `g`
60 _vm, _gself, _dest, args, kwargs = vm.switch(glet, gself, args, kwargs) 60 _vm, _gself, _dest, args, kwargs = vm.switch(glet, gself, args, kwargs)
61 assert _gself is gself 61 assert _gself is gself
62 assert _dest is None # instructions can't tell us where to jump 62 assert _dest is None # instructions can't tell us where to jump
76 fn must accept as the first positional argument this greenlet itself, which can be used as 76 fn must accept as the first positional argument this greenlet itself, which can be used as
77 the return-jump location for internal greenlet switches (ideally using gswitch). 77 the return-jump location for internal greenlet switches (ideally using gswitch).
78 """ 78 """
79 def service_loop(vm, gself, dest, args, kwargs): 79 def service_loop(vm, gself, dest, args, kwargs):
80 while True: 80 while True:
81 print 'service calling', fn.__name__, args, kwargs 81 #print 'service calling', fn.__name__, args, kwargs
82 t = fn(vm, gself, *args, **kwargs) 82 t = fn(vm, gself, *args, **kwargs)
83 #TODO consider a protocol for returning args, kwargs 83 #TODO consider a protocol for returning args, kwargs
84 if t is None: 84 if t is None:
85 _vm,_gself,dest, args, kwargs = vm.switch(dest, None, (), {}) 85 _vm,_gself,dest, args, kwargs = vm.switch(dest, None, (), {})
86 else: 86 else: