Tasks¶
Like Starlette and any other Starlette based frameworks, in Esmerald you can define background tasks to run after the returning response.
You can use the Task and Task with any ASGI framework as if it was native.
This can be useful for those operations that need to happen after the request without blocking the client (the client doesn't have to wait to complete) from receiving that same response.
Also you can simply run them as non-blocking operations without relying on any ASGI framework, simple Python background tasks.
backgrounder.Task ¶
Task(func, *args, **kwargs)
Bases: Repr
Task
as a single instance can be easily achieved.
Example
from backgrounder import Task
async def send_email_notification(message: str):
'''
Sends an email notification
'''
send_notification(message)
task = Task(send_email_notification, "message to someone")
PARAMETER | DESCRIPTION |
---|---|
func |
Any callable to be executed by in the background.
This can be Example
TYPE:
|
*args |
Any arguments of the callable. Example
TYPE:
|
**kwargs |
Any kwyword arguments of the callable. Example
TYPE:
|
Source code in backgrounder/tasks.py
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 |
|
backgrounder.Tasks ¶
Tasks(tasks=None, as_group=False)
Bases: Task
Alternatively, the Tasks
can also be used to be passed
in.
Example
from datetime import datetime
from backgrounder import Task, Tasks
async def send_email_notification(message: str):
'''
Sends an email notification
'''
send_notification(message)
def write_in_file():
with open("log.txt", mode="w") as log:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
content = f"Notification sent @ {now}"
log.write(content)
tasks = Tasks([
Task(send_email_notification, message="Account created"),
Task(write_in_file),
])
await tasks()
When as_group
is set to True, it will run all the tasks concurrently (as a group)
Example
from datetime import datetime
from backgrounder import Task, Tasks
async def send_email_notification(message: str):
'''
Sends an email notification
'''
send_notification(message)
def write_in_file():
with open("log.txt", mode="w") as log:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
content = f"Notification sent @ {now}"
log.write(content)
tasks = Tasks([
Task(send_email_notification, message="Account created"),
Task(write_in_file),
], as_group=True)
await tasks()
PARAMETER | DESCRIPTION |
---|---|
tasks |
A Example
TYPE:
|
as_group |
Boolean flag indicating if the tasks should be run concurrently, in other words, as a group. Example
TYPE:
|
Source code in backgrounder/tasks.py
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 |
|
add_task ¶
add_task(func, *args, **kwargs)
Another way of adding tasks to the Tasks
object.
Example
from datetime import datetime
from backgrounder import Task, Tasks
async def send_email_notification(message: str):
'''
Sends an email notification
'''
send_notification(message)
def write_in_file():
with open("log.txt", mode="w") as log:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
content = f"Notification sent @ {now}"
log.write(content)
tasks = Tasks()
tasks.add_task(send_email_notification, message="Account created")
tasks.add_task(write_in_file)
await tasks()
Or if you want to run them concurrently.
Example
from datetime import datetime
from backgrounder import Task, Tasks
async def send_email_notification(message: str):
'''
Sends an email notification
'''
send_notification(message)
def write_in_file():
with open("log.txt", mode="w") as log:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
content = f"Notification sent @ {now}"
log.write(content)
tasks = Tasks(as_group=True)
tasks.add_task(send_email_notification, message="Account created")
tasks.add_task(write_in_file)
await tasks()
PARAMETER | DESCRIPTION |
---|---|
func |
TYPE:
|
*args |
TYPE:
|
**kwargs |
TYPE:
|
Source code in backgrounder/tasks.py
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
|