Skip to content

Commit 0ab40c3

Browse files
authored
Merge pull request #691 from AnswerDotAI/revert-674-background-tasks
Revert "Fix+docs for background tasks when used by FtResponse"
2 parents 32ca38c + c6b029a commit 0ab40c3

File tree

3 files changed

+9
-381
lines changed

3 files changed

+9
-381
lines changed

fasthtml/core.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,13 +775,12 @@ async def __call__(self, scope, receive, send) -> None:
775775
# %% ../nbs/api/00_core.ipynb
776776
class FtResponse:
777777
"Wrap an FT response with any Starlette `Response`"
778-
def __init__(self, content, status_code:int=200, headers=None, cls=HTMLResponse, media_type:str|None=None, background: BackgroundTask | None = None):
778+
def __init__(self, content, status_code:int=200, headers=None, cls=HTMLResponse, media_type:str|None=None):
779779
self.content,self.status_code,self.headers = content,status_code,headers
780-
self.cls,self.media_type,self.background = cls,media_type,background
780+
self.cls,self.media_type = cls,media_type
781781

782782
def __response__(self, req):
783783
cts,httphdrs,tasks = _xt_cts(req, self.content)
784-
if not tasks.tasks: tasks = self.background
785784
headers = {**(self.headers or {}), **httphdrs}
786785
return self.cls(cts, status_code=self.status_code, headers=headers, media_type=self.media_type, background=tasks)
787786

nbs/api/00_core.ipynb

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"datetime.datetime(2025, 3, 20, 14, 0)"
134+
"datetime.datetime(2025, 3, 16, 14, 0)"
135135
]
136136
},
137137
"execution_count": null,
@@ -1231,7 +1231,7 @@
12311231
{
12321232
"data": {
12331233
"text/plain": [
1234-
"'a2597a39-e99d-4460-90b9-2ca21e87bc8f'"
1234+
"'cc87253c-bfc1-4544-bbc0-58dd8d3291bc'"
12351235
]
12361236
},
12371237
"execution_count": null,
@@ -2578,13 +2578,13 @@
25782578
"name": "stdout",
25792579
"output_type": "stream",
25802580
"text": [
2581-
"Set to 2025-03-20 09:00:21.844393\n"
2581+
"Set to 2025-03-16 04:41:09.011712\n"
25822582
]
25832583
},
25842584
{
25852585
"data": {
25862586
"text/plain": [
2587-
"'Session time: 2025-03-20 09:00:21.844393'"
2587+
"'Session time: 2025-03-16 04:41:09.011712'"
25882588
]
25892589
},
25902590
"execution_count": null,
@@ -3105,7 +3105,7 @@
31053105
{
31063106
"data": {
31073107
"text/plain": [
3108-
"'Cookie was set at time 09:00:22.051066'"
3108+
"'Cookie was set at time 04:41:09.620176'"
31093109
]
31103110
},
31113111
"execution_count": null,
@@ -3236,13 +3236,12 @@
32363236
"#| export\n",
32373237
"class FtResponse:\n",
32383238
" \"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",
32403240
" 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",
32423242
"\n",
32433243
" def __response__(self, req):\n",
32443244
" cts,httphdrs,tasks = _xt_cts(req, self.content)\n",
3245-
" if not tasks.tasks: tasks = self.background\n",
32463245
" headers = {**(self.headers or {}), **httphdrs}\n",
32473246
" return self.cls(cts, status_code=self.status_code, headers=headers, media_type=self.media_type, background=tasks)"
32483247
]
@@ -3267,88 +3266,6 @@
32673266
"assert '<title>Foo</title>' in txt and '<h1>bar</h1>' in txt and '<html>' in txt"
32683267
]
32693268
},
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-
},
33523269
{
33533270
"cell_type": "code",
33543271
"execution_count": null,

0 commit comments

Comments
 (0)