annotate Demo/threads/Coroutine.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents f2901b6ac5ed
children 71a294bd7950
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15388
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Coroutine implementation using Python threads.
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Combines ideas from Guido's Generator module, and from the coroutine
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # features of Icon and Simula 67.
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # To run a collection of functions as coroutines, you need to create
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # a Coroutine object to control them:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # co = Coroutine()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # and then 'create' a subsidiary object for each function in the
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # collection:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # cof1 = co.create(f1 [, arg1, arg2, ...]) # [] means optional,
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # cof2 = co.create(f2 [, arg1, arg2, ...]) #... not list
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # cof3 = co.create(f3 [, arg1, arg2, ...])
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # etc. The functions need not be distinct; 'create'ing the same
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # function multiple times gives you independent instances of the
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # function.
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # To start the coroutines running, use co.tran on one of the create'd
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # functions; e.g., co.tran(cof2). The routine that first executes
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # co.tran is called the "main coroutine". It's special in several
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # respects: it existed before you created the Coroutine object; if any of
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # the create'd coroutines exits (does a return, or suffers an unhandled
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # exception), EarlyExit error is raised in the main coroutine; and the
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # co.detach() method transfers control directly to the main coroutine
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # (you can't use co.tran() for this because the main coroutine doesn't
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # have a name ...).
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # Coroutine objects support these methods:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # handle = .create(func [, arg1, arg2, ...])
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # Creates a coroutine for an invocation of func(arg1, arg2, ...),
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 # and returns a handle ("name") for the coroutine so created. The
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 # handle can be used as the target in a subsequent .tran().
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 # .tran(target, data=None)
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 # Transfer control to the create'd coroutine "target", optionally
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 # passing it an arbitrary piece of data. To the coroutine A that does
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # the .tran, .tran acts like an ordinary function call: another
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 # coroutine B can .tran back to it later, and if it does A's .tran
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 # returns the 'data' argument passed to B's tran. E.g.,
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 # in coroutine coA in coroutine coC in coroutine coB
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 # x = co.tran(coC) co.tran(coB) co.tran(coA,12)
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 # print x # 12
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 # The data-passing feature is taken from Icon, and greatly cuts
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 # the need to use global variables for inter-coroutine communication.
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 # .back( data=None )
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 # The same as .tran(invoker, data=None), where 'invoker' is the
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 # coroutine that most recently .tran'ed control to the coroutine
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 # doing the .back. This is akin to Icon's "&source".
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # .detach( data=None )
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 # The same as .tran(main, data=None), where 'main' is the
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 # (unnameable!) coroutine that started it all. 'main' has all the
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 # rights of any other coroutine: upon receiving control, it can
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # .tran to an arbitrary coroutine of its choosing, go .back to
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 # the .detach'er, or .kill the whole thing.
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 #
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 # .kill()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 # Destroy all the coroutines, and return control to the main
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 # coroutine. None of the create'ed coroutines can be resumed after a
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # .kill(). An EarlyExit exception does a .kill() automatically. It's
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 # a good idea to .kill() coroutines you're done with, since the
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 # current implementation consumes a thread for each coroutine that
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 # may be resumed.
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 import thread
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 import sync
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 class _CoEvent:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 def __init__(self, func):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 self.f = func
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 self.e = sync.event()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 def __repr__(self):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 if self.f is None:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 return 'main coroutine'
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 else:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 return 'coroutine for func ' + self.f.func_name
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 def __hash__(self):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 return id(self)
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 def __cmp__(x,y):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 return cmp(id(x), id(y))
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 def resume(self):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 self.e.post()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 def wait(self):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 self.e.wait()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 self.e.clear()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 Killed = 'Coroutine.Killed'
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 EarlyExit = 'Coroutine.EarlyExit'
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 class Coroutine:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 def __init__(self):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 self.active = self.main = _CoEvent(None)
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 self.invokedby = {self.main: None}
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 self.killed = 0
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 self.value = None
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 self.terminated_by = None
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 def create(self, func, *args):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 me = _CoEvent(func)
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 self.invokedby[me] = None
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 thread.start_new_thread(self._start, (me,) + args)
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 return me
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 def _start(self, me, *args):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 me.wait()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 if not self.killed:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 try:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 try:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 apply(me.f, args)
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 except Killed:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 pass
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 finally:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 if not self.killed:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 self.terminated_by = me
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 self.kill()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 def kill(self):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 if self.killed:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 raise TypeError, 'kill() called on dead coroutines'
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 self.killed = 1
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 for coroutine in self.invokedby.keys():
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 coroutine.resume()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 def back(self, data=None):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 return self.tran( self.invokedby[self.active], data )
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
135
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 def detach(self, data=None):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 return self.tran( self.main, data )
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 def tran(self, target, data=None):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 if not self.invokedby.has_key(target):
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 raise TypeError, '.tran target ' + `target` + \
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 ' is not an active coroutine'
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 if self.killed:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 raise TypeError, '.tran target ' + `target` + ' is killed'
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 self.value = data
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 me = self.active
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 self.invokedby[target] = me
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 self.active = target
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 target.resume()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 me.wait()
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 if self.killed:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 if self.main is not me:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 raise Killed
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 if self.terminated_by is not None:
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 raise EarlyExit, `self.terminated_by` + ' terminated early'
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 return self.value
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
f2901b6ac5ed Add 1994 Coroutine module by Tim Peters
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 # end of module