question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It would be nice to have a simple API to direct post-response cleanup efforts outside of the task. Consider the following example:

async def cleanup(request: Request, response: HTTPResponse):
    print("sleeping")
    await sleep(1)
    print("done")


@app.get("/")
async def handler(request: Request):
    response = await request.respond()
    await response.send("foobar")
    await response.eof()
    response.cleanup(cleanup)

In this example, we would immediately schedule a task using add_task API to allow the response to return as early as possible. But, it should also be smart enough to work even without eof() being called such that it would wait for the response to be sent and cleaned before proceeding to cleanup. Then you could even have handlers like this and guarantee that cleanup is run on a completed response.

async def cleanup(request: Request, response: HTTPResponse):
    print("sleeping")
    await sleep(1)
    print("done")


@app.get("/")
async def handler(request: Request):
    resp = json({"foo": "bar"})
    resp.cleanup(cleanup)
    return resp

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ahopkinscommented, Oct 27, 2022

You are correct. Scrap the shortcut and let’s fix this for real.

@app.get("/")
async def handler(request: Request):
    response = await request.respond()
    await response.send("foobar")
    await response.eof()

    # Client is free to close connection or start another request
    await do_something_here()
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Connection closed β”œβ”€β”€β”€β–Ίβ”‚ Is in handler? β”œβ”€β”€Y─►│ Eof sent? β”œβ”€β”€Y─►│ Complete β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚                  β”‚
                                 No                 N
                                 β”‚                  β”‚
                                 β–Ό                  β”‚
                              β”Œβ”€β”€β”€β”€β”€β”€β”              β”‚
                              β”‚Cancelβ”‚β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β””β”€β”€β”€β”€β”€β”€β”˜
0reactions
Troniccommented, Oct 27, 2022

I have no take on that. Only that sending the response inside handler was supposed to be first class API, and after-handler handling only a fallback that calls it (even if in practice most handlers would return a response, not send it).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Response, Cleanup, and Reporting Procedures
Describes procedure for response, cleanup, and reporting for all types of spills.
Read more >
Natural Disaster Response and Cleanup - Princeton EHS
Cleanup and recovery activities involve exposure to hazardous conditions including contaminated floodwater, downed electrical power lines, mold, tree-trimmingΒ ...
Read more >
Spill Response and Clean-Up
Major Chemical Spill* Β· Beyond capabilities and resources to clean Β· Large amount (volume) Β· Highly hazardous substance Β· Emits harmful/toxic vapor, gas,...
Read more >
EPA's Response Techniques | US EPA
Temporarily moving residents affected by hazardous substance contamination while cleanup activities take place; and/or; Installing fences to prevent directΒ ...
Read more >
Tornado Cleanup and Response | NIOSH - CDC
During cleanup activities physical, chemical and biological hazards may be present. Challenges faced by emergency response and recovery workers includeΒ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found