0
# Core Concurrency
1
2
Essential green threading primitives for managing cooperative concurrency in eventlet applications. These functions provide the foundation for creating, controlling, and managing greenthreads.
3
4
## Capabilities
5
6
### Greenthread Creation
7
8
Create new greenthreads to run functions concurrently in a cooperative manner.
9
10
```python { .api }
11
def spawn(func, *args, **kwargs):
12
"""
13
Create a greenthread to run func(*args, **kwargs).
14
15
Parameters:
16
- func: callable to run in new greenthread
17
- *args: positional arguments to pass to func
18
- **kwargs: keyword arguments to pass to func
19
20
Returns:
21
GreenThread instance that can be used to retrieve return value
22
"""
23
24
def spawn_n(func, *args, **kwargs):
25
"""
26
Create a greenthread to run func(*args, **kwargs) without return value access.
27
28
Same as spawn() but doesn't return a GreenThread object, making it slightly
29
more efficient when you don't need the return value.
30
31
Parameters:
32
- func: callable to run in new greenthread
33
- *args: positional arguments to pass to func
34
- **kwargs: keyword arguments to pass to func
35
36
Returns:
37
None
38
"""
39
40
def spawn_after(seconds, func, *args, **kwargs):
41
"""
42
Spawn a greenthread to run func(*args, **kwargs) after a delay.
43
44
Parameters:
45
- seconds: float, delay in seconds before spawning
46
- func: callable to run in new greenthread
47
- *args: positional arguments to pass to func
48
- **kwargs: keyword arguments to pass to func
49
50
Returns:
51
GreenThread instance
52
"""
53
54
def spawn_after_local(seconds, func, *args, **kwargs):
55
"""
56
Spawn a greenthread after a delay, but only if current greenthread is still alive.
57
58
Unlike spawn_after, the function will NOT be called if the current
59
greenthread has exited by the time the delay expires.
60
61
Parameters:
62
- seconds: float, delay in seconds before spawning
63
- func: callable to run in new greenthread
64
- *args: positional arguments to pass to func
65
- **kwargs: keyword arguments to pass to func
66
67
Returns:
68
GreenThread instance
69
"""
70
```
71
72
### Greenthread Control
73
74
Control the execution flow and lifecycle of greenthreads.
75
76
```python { .api }
77
def sleep(seconds=0):
78
"""
79
Yield control to other greenthreads for the specified time.
80
81
Parameters:
82
- seconds: float, time to sleep in seconds (default: 0 for immediate yield)
83
84
Returns:
85
None
86
"""
87
88
def kill(gt, exc_type=None):
89
"""
90
Terminate a greenthread by raising an exception in it.
91
92
Parameters:
93
- gt: GreenThread instance to terminate
94
- exc_type: exception class to raise (default: greenlet.GreenletExit)
95
96
Returns:
97
None
98
"""
99
100
def getcurrent():
101
"""
102
Get the current greenthread.
103
104
Returns:
105
Current greenlet/greenthread instance
106
"""
107
```
108
109
### Delayed Execution
110
111
Schedule functions and exceptions to run after delays.
112
113
```python { .api }
114
def exc_after(seconds, exc_type, *args, **kwargs):
115
"""
116
Schedule an exception to be raised in the current greenthread after delay.
117
118
Parameters:
119
- seconds: float, delay in seconds
120
- exc_type: exception class to raise
121
- *args: arguments to pass to exception constructor
122
- **kwargs: keyword arguments to pass to exception constructor
123
124
Returns:
125
Timer object that can be cancelled
126
"""
127
128
def call_after_global(seconds, func, *args, **kwargs):
129
"""
130
Schedule a function to be called after a delay.
131
132
Parameters:
133
- seconds: float, delay in seconds
134
- func: callable to run after delay
135
- *args: positional arguments to pass to func
136
- **kwargs: keyword arguments to pass to func
137
138
Returns:
139
Timer object that can be cancelled
140
"""
141
142
def spawn_after_local(seconds, func, *args, **kwargs):
143
"""
144
Spawn a greenthread after delay, cancelled if current greenthread exits.
145
146
Parameters:
147
- seconds: float, delay in seconds
148
- func: callable to run in new greenthread
149
- *args: positional arguments to pass to func
150
- **kwargs: keyword arguments to pass to func
151
152
Returns:
153
GreenThread instance
154
"""
155
```
156
157
## Usage Examples
158
159
```python
160
import eventlet
161
162
def worker(name, duration):
163
"""A simple worker function"""
164
print(f"Worker {name} starting")
165
eventlet.sleep(duration)
166
print(f"Worker {name} finished")
167
return f"Result from {name}"
168
169
# Spawn greenthreads and wait for results
170
gt1 = eventlet.spawn(worker, "A", 1.0)
171
gt2 = eventlet.spawn(worker, "B", 0.5)
172
173
# Spawn without return value access (more efficient)
174
eventlet.spawn_n(worker, "C", 2.0)
175
176
# Get results from greenthreads
177
result1 = gt1.wait() # Blocks until complete
178
result2 = gt2.wait()
179
180
print(f"Results: {result1}, {result2}")
181
182
# Delayed execution
183
eventlet.spawn_after(3.0, worker, "Delayed", 1.0)
184
185
# Schedule exception after delay
186
eventlet.exc_after(10.0, TimeoutError, "Operation timed out")
187
188
# Yield control to other greenthreads
189
eventlet.sleep(0) # Immediate yield
190
eventlet.sleep(1.0) # Sleep for 1 second
191
```
192
193
## Types
194
195
```python { .api }
196
class GreenThread(greenlet.greenlet):
197
"""
198
A greenlet subclass that can be used to retrieve return values and
199
provides additional lifecycle management methods.
200
"""
201
202
def wait(self):
203
"""
204
Wait for the greenthread to complete and return its result.
205
206
Returns:
207
The return value of the wrapped function
208
209
Raises:
210
Any exception raised by the wrapped function
211
"""
212
213
def kill(self, exc_type=None):
214
"""
215
Terminate this greenthread by raising an exception.
216
217
Parameters:
218
- exc_type: exception class to raise (default: greenlet.GreenletExit)
219
"""
220
221
def cancel(self, exc_type=None):
222
"""
223
Cancel this greenthread if it hasn't started running yet.
224
225
Parameters:
226
- exc_type: exception class to raise (default: greenlet.GreenletExit)
227
228
Note:
229
Has no effect if the greenthread has already started execution.
230
"""
231
232
def link(self, func, *curried_args, **curried_kwargs):
233
"""
234
Register a callback to be called when this greenthread exits.
235
236
Parameters:
237
- func: callable to invoke when greenthread exits
238
- *curried_args: additional args to pass to func
239
- **curried_kwargs: additional kwargs to pass to func
240
"""
241
242
def unlink(self, func, *curried_args, **curried_kwargs):
243
"""
244
Remove a previously registered callback.
245
246
Parameters:
247
- func: callable previously registered with link()
248
- *curried_args: args that were curried with the callback
249
- **curried_kwargs: kwargs that were curried with the callback
250
"""
251
252
def ready(self):
253
"""
254
Check if the greenthread has completed execution.
255
256
Returns:
257
bool: True if complete, False if still running
258
"""
259
260
def successful(self):
261
"""
262
Check if the greenthread completed successfully without exception.
263
264
Returns:
265
bool: True if successful, False if raised exception or still running
266
"""
267
```