|
131 | 131 | { |
132 | 132 | "data": { |
133 | 133 | "text/plain": [ |
134 | | - "datetime.datetime(2025, 3, 20, 14, 0)" |
| 134 | + "datetime.datetime(2025, 3, 16, 14, 0)" |
135 | 135 | ] |
136 | 136 | }, |
137 | 137 | "execution_count": null, |
|
1231 | 1231 | { |
1232 | 1232 | "data": { |
1233 | 1233 | "text/plain": [ |
1234 | | - "'a2597a39-e99d-4460-90b9-2ca21e87bc8f'" |
| 1234 | + "'cc87253c-bfc1-4544-bbc0-58dd8d3291bc'" |
1235 | 1235 | ] |
1236 | 1236 | }, |
1237 | 1237 | "execution_count": null, |
|
2578 | 2578 | "name": "stdout", |
2579 | 2579 | "output_type": "stream", |
2580 | 2580 | "text": [ |
2581 | | - "Set to 2025-03-20 09:00:21.844393\n" |
| 2581 | + "Set to 2025-03-16 04:41:09.011712\n" |
2582 | 2582 | ] |
2583 | 2583 | }, |
2584 | 2584 | { |
2585 | 2585 | "data": { |
2586 | 2586 | "text/plain": [ |
2587 | | - "'Session time: 2025-03-20 09:00:21.844393'" |
| 2587 | + "'Session time: 2025-03-16 04:41:09.011712'" |
2588 | 2588 | ] |
2589 | 2589 | }, |
2590 | 2590 | "execution_count": null, |
|
3105 | 3105 | { |
3106 | 3106 | "data": { |
3107 | 3107 | "text/plain": [ |
3108 | | - "'Cookie was set at time 09:00:22.051066'" |
| 3108 | + "'Cookie was set at time 04:41:09.620176'" |
3109 | 3109 | ] |
3110 | 3110 | }, |
3111 | 3111 | "execution_count": null, |
|
3236 | 3236 | "#| export\n", |
3237 | 3237 | "class FtResponse:\n", |
3238 | 3238 | " \"Wrap an FT response with any Starlette `Response`\"\n", |
3239 | | - " def __init__(self, content, status_code:int=200, headers=None, cls=HTMLResponse, media_type:str|None=None, background: BackgroundTask | None = None):\n", |
| 3239 | + " def __init__(self, content, status_code:int=200, headers=None, cls=HTMLResponse, media_type:str|None=None):\n", |
3240 | 3240 | " self.content,self.status_code,self.headers = content,status_code,headers\n", |
3241 | | - " self.cls,self.media_type,self.background = cls,media_type,background\n", |
| 3241 | + " self.cls,self.media_type = cls,media_type\n", |
3242 | 3242 | "\n", |
3243 | 3243 | " def __response__(self, req):\n", |
3244 | 3244 | " cts,httphdrs,tasks = _xt_cts(req, self.content)\n", |
3245 | | - " if not tasks.tasks: tasks = self.background\n", |
3246 | 3245 | " headers = {**(self.headers or {}), **httphdrs}\n", |
3247 | 3246 | " return self.cls(cts, status_code=self.status_code, headers=headers, media_type=self.media_type, background=tasks)" |
3248 | 3247 | ] |
|
3267 | 3266 | "assert '<title>Foo</title>' in txt and '<h1>bar</h1>' in txt and '<html>' in txt" |
3268 | 3267 | ] |
3269 | 3268 | }, |
3270 | | - { |
3271 | | - "cell_type": "markdown", |
3272 | | - "id": "3f506103", |
3273 | | - "metadata": {}, |
3274 | | - "source": [ |
3275 | | - "Test on a single background task:" |
3276 | | - ] |
3277 | | - }, |
3278 | | - { |
3279 | | - "cell_type": "code", |
3280 | | - "execution_count": null, |
3281 | | - "id": "4ea66093", |
3282 | | - "metadata": {}, |
3283 | | - "outputs": [ |
3284 | | - { |
3285 | | - "name": "stdout", |
3286 | | - "output_type": "stream", |
3287 | | - "text": [ |
3288 | | - "Starting slow task\n", |
3289 | | - "Finished slow task\n" |
3290 | | - ] |
3291 | | - } |
3292 | | - ], |
3293 | | - "source": [ |
3294 | | - "def my_slow_task():\n", |
3295 | | - " print('Starting slow task') \n", |
3296 | | - " time.sleep(3)\n", |
3297 | | - " print('Finished slow task') \n", |
3298 | | - "\n", |
3299 | | - "@rt('/background')\n", |
3300 | | - "def get():\n", |
3301 | | - " return FtResponse(P('BG Task'), background=BackgroundTask(my_slow_task))\n", |
3302 | | - "\n", |
3303 | | - "r = cli.get('/background')\n", |
3304 | | - "\n", |
3305 | | - "test_eq(r.status_code, 200)\n" |
3306 | | - ] |
3307 | | - }, |
3308 | | - { |
3309 | | - "cell_type": "markdown", |
3310 | | - "id": "881a88bb", |
3311 | | - "metadata": {}, |
3312 | | - "source": [ |
3313 | | - "Test multiple background tasks:" |
3314 | | - ] |
3315 | | - }, |
3316 | | - { |
3317 | | - "cell_type": "code", |
3318 | | - "execution_count": null, |
3319 | | - "id": "1764a6aa", |
3320 | | - "metadata": {}, |
3321 | | - "outputs": [ |
3322 | | - { |
3323 | | - "name": "stdout", |
3324 | | - "output_type": "stream", |
3325 | | - "text": [ |
3326 | | - "Sleeping for 0s\n", |
3327 | | - "Slept for 0s\n", |
3328 | | - "Sleeping for 1s\n", |
3329 | | - "Slept for 1s\n", |
3330 | | - "Sleeping for 2s\n", |
3331 | | - "Slept for 2s\n" |
3332 | | - ] |
3333 | | - } |
3334 | | - ], |
3335 | | - "source": [ |
3336 | | - "def increment(amount):\n", |
3337 | | - " print(f'Sleeping for {amount}s') \n", |
3338 | | - " time.sleep(amount)\n", |
3339 | | - " print(f'Slept for {amount}s') \n", |
3340 | | - "\n", |
3341 | | - "@rt('/backgrounds')\n", |
3342 | | - "def get():\n", |
3343 | | - " tasks = BackgroundTasks()\n", |
3344 | | - " for i in range(3): tasks.add_task(increment, i)\n", |
3345 | | - " return FtResponse(P('BG Tasks'), background=tasks)\n", |
3346 | | - "\n", |
3347 | | - "r = cli.get('/backgrounds')\n", |
3348 | | - "\n", |
3349 | | - "test_eq(r.status_code, 200)" |
3350 | | - ] |
3351 | | - }, |
3352 | 3269 | { |
3353 | 3270 | "cell_type": "code", |
3354 | 3271 | "execution_count": null, |
|
0 commit comments