-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdirect_handler.py
More file actions
325 lines (294 loc) · 8.97 KB
/
direct_handler.py
File metadata and controls
325 lines (294 loc) · 8.97 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"""
Direct Handler Example
=======================
This example demonstrates the DirectTaskHandler - the default execution handler
in Graflow. The DirectTaskHandler executes tasks in-process within the same
Python interpreter, providing the fastest execution with no overhead.
Concepts Covered:
-----------------
1. Default handler behavior
2. Explicit handler specification
3. In-process execution characteristics
4. Performance benefits
5. When to use DirectTaskHandler
Expected Output:
----------------
=== Direct Handler Demo ===
Starting execution from: task_default
✅ Default Task (no handler specified)
Execution environment: Same process as main
Handler type: direct (default)
✅ Explicit Direct Task
Execution environment: Same process as main
Handler type: direct (explicit)
✅ Fast Computation Task
Computing Fibonacci(30)...
Result: 832040
Execution time: ~0.XXX seconds
Execution completed after 3 steps
=== Summary ===
All tasks executed in-process with DirectTaskHandler
- No containerization overhead
- Shared memory space
- Fast execution
- Perfect for most use cases! ✅
"""
import time
from graflow.core.decorators import task
from graflow.core.workflow import workflow
def fibonacci(n: int) -> int:
"""Calculate Fibonacci number (recursive for demonstration)."""
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
def main():
"""Demonstrate DirectTaskHandler usage."""
print("=== Direct Handler Demo ===\n")
with workflow("direct_handler_demo") as ctx:
@task
def task_default():
"""
Task without explicit handler specification.
When no handler is specified, DirectTaskHandler is used by default.
This is the recommended approach for most tasks.
"""
print("✅ Default Task (no handler specified)")
print(" Execution environment: Same process as main")
print(" Handler type: direct (default)\n")
return "default_result"
@task(handler="direct")
def task_explicit():
"""
Task with explicit handler="direct" specification.
This is equivalent to omitting the handler parameter.
Use this when you want to be explicit about execution mode.
"""
print("✅ Explicit Direct Task")
print(" Execution environment: Same process as main")
print(" Handler type: direct (explicit)\n")
return "explicit_result"
@task(handler="direct")
def task_performance():
"""
Demonstrate the performance benefits of DirectTaskHandler.
DirectTaskHandler has:
- No containerization overhead
- No serialization/deserialization
- Native Python performance
"""
print("✅ Fast Computation Task")
print(" Computing Fibonacci(30)...")
start = time.time()
result = fibonacci(30)
elapsed = time.time() - start
print(f" Result: {result}")
print(f" Execution time: ~{elapsed:.3f} seconds\n")
return result
# Define workflow
task_default >> task_explicit >> task_performance
# Execute
ctx.execute("task_default")
# Summary
print("=== Summary ===")
print("All tasks executed in-process with DirectTaskHandler")
print("- No containerization overhead")
print("- Shared memory space")
print("- Fast execution")
print("- Perfect for most use cases! ✅")
if __name__ == "__main__":
main()
# ============================================================================
# Key Takeaways:
# ============================================================================
#
# 1. **Default Handler**
# @task
# def my_task():
# pass
#
# - DirectTaskHandler is the default when no handler is specified
# - No need to explicitly set handler="direct" in most cases
# - Simplest and most common execution mode
#
# 2. **Explicit Specification**
# @task(handler="direct")
# def my_task():
# pass
#
# - Equivalent to omitting the handler parameter
# - Use when you want to be explicit about execution mode
# - Useful for documentation and clarity
#
# 3. **In-Process Execution**
# - Tasks run in the same Python process
# - Shares memory space with main program
# - Can access module-level variables
# - No isolation between tasks
#
# 4. **Performance Characteristics**
# - Fastest execution (no overhead)
# - No serialization/deserialization
# - No container startup time
# - Native Python performance
#
# 5. **When to Use DirectTaskHandler**
# ✅ Trusted code
# ✅ Local development
# ✅ Fast iteration
# ✅ Most production tasks
# ✅ Tasks that don't need isolation
# ✅ Performance-critical operations
#
# 6. **When NOT to Use DirectTaskHandler**
# ❌ Untrusted code (use Docker)
# ❌ Need process isolation (use Docker)
# ❌ Different Python versions (use Docker)
# ❌ Specific system dependencies (use Docker)
# ❌ Remote execution (use custom handler)
#
# 7. **Shared State**
# # Module-level variables are shared
# counter = 0
#
# @task
# def increment():
# global counter
# counter += 1 # Modifies shared state
#
# @task
# def read():
# return counter # Reads shared state
#
# - Be careful with shared mutable state
# - Use channels for explicit communication
#
# ============================================================================
# Try Experimenting:
# ============================================================================
#
# 1. Compare with Docker handler (next example):
# @task(handler="direct")
# def fast():
# return "instant"
#
# @task(handler="docker") # Much slower
# def slow():
# return "delayed"
#
# 2. Test shared state:
# global_list = []
#
# @task
# def append_task():
# global_list.append(1)
#
# @task
# def read_task():
# return len(global_list)
#
# 3. Measure execution overhead:
# import time
#
# @task
# def timed_task():
# start = time.time()
# # Do work
# return time.time() - start
#
# 4. Access imported modules:
# import numpy as np
#
# @task
# def use_numpy():
# return np.array([1, 2, 3]).sum()
#
# 5. Use closures:
# def create_task(multiplier):
# @task
# def multiply(x):
# return x * multiplier
# return multiply
#
# task_double = create_task(2)
# result = task_double(5) # Returns 10
#
# ============================================================================
# Real-World Use Cases:
# ============================================================================
#
# **Data Processing Pipeline**:
# All tasks run in-process for maximum performance
#
# **Web API Backend**:
# Task-based request handlers with fast execution
#
# **Local Machine Learning**:
# Training and inference tasks without containerization overhead
#
# **Database Operations**:
# Query execution tasks with shared connection pool
#
# **File Processing**:
# Read, transform, write tasks with direct filesystem access
#
# **Real-time Systems**:
# Low-latency task execution for time-critical operations
#
# ============================================================================
# Advanced Topics:
# ============================================================================
#
# **Memory Sharing**:
# @task
# def producer():
# return large_dataset # Passed by reference (no copy)
#
# @task
# def consumer(data):
# # Receives reference to same object
# return process(data)
#
# **Exception Handling**:
# @task
# def may_fail():
# try:
# risky_operation()
# except Exception as e:
# # Exception is caught and stored in context
# raise
#
# **Resource Management**:
# @task
# def use_resources():
# with open("file.txt") as f:
# return f.read()
# # File automatically closed
#
# **Threading Compatibility**:
# @task
# def threaded():
# import threading
# # Can use threading within tasks
# thread = threading.Thread(target=worker)
# thread.start()
# thread.join()
#
# ============================================================================
# Comparison: Direct vs Other Handlers
# ============================================================================
#
# | Feature | Direct | Docker | Custom |
# |---------|--------|--------|--------|
# | Speed | Fastest | Slow | Varies |
# | Isolation | None | Full | Configurable |
# | Overhead | ~0ms | ~500-2000ms | Varies |
# | Setup | None | Docker required | Custom |
# | Use Case | Most tasks | Isolation | Special needs |
#
# **Recommendation**: Start with DirectTaskHandler for all tasks.
# Only use other handlers when you have a specific need for:
# - Process isolation (Docker)
# - Remote execution (Custom)
# - Different environments (Docker)
#
# ============================================================================