-
Notifications
You must be signed in to change notification settings - Fork 7
/
intro-to-stackless.txt
executable file
·190 lines (166 loc) · 5.92 KB
/
intro-to-stackless.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
==============================
Getting Started With Stackless
==============================
This chapter provides a brief introduction to stackless' primitives. Later
chapters will build on these primitives to show more practical functionality.
--------
Tasklets
--------
Tasklets are the primary building block for stackless. You create a tasklet by
feeding it any python callable (usually a function or class method). This
creates a tasklet and adds it to the scheduler. Here is a quick
demonstration::
Python 2.4.3 Stackless 3.1b3 060504 (#69, May 3 2006, 19:20:41) [MSC v.1310 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import stackless
>>>
>>> def print_x(x):
... print x
...
>>> stackless.tasklet(print_x)('one')
<stackless.tasklet object at 0x00A45870>
>>> stackless.tasklet(print_x)('two')
<stackless.tasklet object at 0x00A45A30>
>>> stackless.tasklet(print_x)('three')
<stackless.tasklet object at 0x00A45AB0>
>>>
>>> stackless.run()
one
two
three
>>>
Note that the tasklets queue up and don't run until you call *stackless.run()*.
-------------
The Scheduler
-------------
The scheduler controls the order in which the tasklets are run. If you just
create a bunch of tasklets, they will run in the order they are created. In
practice, you will generally create tasklets that reschedule themselves so that
each one can have it's turn. A quick demonstration::
Python 2.4.3 Stackless 3.1b3 060504 (#69, May 3 2006, 19:20:41) [MSC v.1310 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import stackless
>>>
>>> def print_three_times(x):
... print "1:", x
... stackless.schedule()
... print "2:", x
... stackless.schedule()
... print "3:", x
... stackless.schedule()
...
>>>
>>> stackless.tasklet(print_three_times)('first')
<stackless.tasklet object at 0x00A45870>
>>> stackless.tasklet(print_three_times)('second')
<stackless.tasklet object at 0x00A45A30>
>>> stackless.tasklet(print_three_times)('third')
<stackless.tasklet object at 0x00A45AB0>
>>>
>>> stackless.run()
1: first
1: second
1: third
2: first
2: second
2: third
3: first
3: second
3: third
>>>
Note that when we call *stackless.schedule()*, the active tasklet pauses
itself and reinserts itself into the end of the scheduler's queue, allowing the
next tasklet to run. Once all of the tasklets ahead of this run, it picks up
where it left off. This continues until all active tasklets have finished
running. This is how we achieve cooperative multitasking with stackless.
--------
Channels
--------
Channels allow you to send information between tasklets. This accomplished two
things:
1. It allows you to exchange information between tasklets.
2. It allows you to control the flow of execution.
Another quick demonstration::
C:\>c:\python24\python
Python 2.4.3 Stackless 3.1b3 060504 (#69, May 3 2006, 19:20:41) [MSC v.1310 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import stackless
>>>
>>> channel = stackless.channel()
>>>
>>> def receiving_tasklet():
... print "Recieving tasklet started"
... print channel.receive()
... print "Receiving tasklet finished"
...
>>> def sending_tasklet():
... print "Sending tasklet started"
... channel.send("send from sending_tasklet")
... print "sending tasklet finished"
...
>>> def another_tasklet():
... print "Just another tasklet in the scheduler"
...
>>> stackless.tasklet(receiving_tasklet)()
<stackless.tasklet object at 0x00A45B30>
>>> stackless.tasklet(sending_tasklet)()
<stackless.tasklet object at 0x00A45B70>
>>> stackless.tasklet(another_tasklet)()
<stackless.tasklet object at 0x00A45BF0>
>>>
>>> stackless.run()
Recieving tasklet started
Sending tasklet started
send from sending_tasklet
Receiving tasklet finished
Just another tasklet in the scheduler
sending tasklet finished
>>>
>>>
When the receiving tasklet calls *channel.receive()*, it blocks. This means
the tasklet pauses until something is sent over the channel. Nothing other
than sending something over the channel will re-activate the tasklet.
When something is sent over the channel by the sending tasklet, the receiving
tasklet resumes immediately, bypassing the existing schedule. The sending
tasklet is send to the back of the schedule, as if *stackless.schedule()* had
been called.
Also note that a send will block if there is nothing to immediately receive the
message::
>>>
>>> stackless.tasklet(sending_tasklet)()
<stackless.tasklet object at 0x00A45B70>
>>> stackless.tasklet(another_tasklet)()
<stackless.tasklet object at 0x00A45BF0>
>>>
>>> stackless.run()
Sending tasklet started
Just another tasklet in the scheduler
>>>
>>> stackless.tasklet(another_tasklet)()
<stackless.tasklet object at 0x00A45B30>
>>> stackless.run()
Just another tasklet in the scheduler
>>>
>>> #Finally adding the receiving tasklet
...
>>> stackless.tasklet(receiving_tasklet)()
<stackless.tasklet object at 0x00A45BF0>
>>>
>>> stackless.run()
Recieving tasklet started
send from sending_tasklet
Receiving tasklet finished
sending tasklet finished
>>>
The sending tasklet will not be re-inserted into the scheduler until it has
successfully send data to another tasklet.
-------
Summary
-------
That covers a majority of stackless' functionality. Doesn't seem like much
does it? We dealt with a couple of objects and maybe 4 or 5 function calls.
By using this small API as our building blocks, we can start to do some really
interesting things.