Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FastAPI server that handles provision requests (sky launch) in the background #2235

Closed
concretevitamin opened this issue Jul 13, 2023 · 6 comments
Labels
friction-log good first issue Good for newcomers help wanted Extra attention is needed Stale

Comments

@concretevitamin
Copy link
Member

concretevitamin commented Jul 13, 2023

A user has the following setup:

  • a FastAPI/gunicorn server that handles provision requests
  • the handler calls sky launch (detach setup + detach run) to provision a cluster to do some work
  • however, this is a blocking call, and it freezes up a worker in the web server for ~5-10 minutes until the cluster is up
  • a few more requests later, this makes the server quickly unresponsive to new requests

We should investigate this usage pattern and add a demo example, perhaps by using “background tasks”: https://fastapi.tiangolo.com/tutorial/background-tasks/

@concretevitamin
Copy link
Member Author

Seems to work well (with detach_setup and detach_run to avoid #2182):

"""Example: a FastAPI server that serves by calling `sky launch`.

Usage:

    $ python server.py

    # In another terminal:
    $ curl localhost:8000
    # Or:
    $ for i in $(seq 32); do curl http://0.0.0.0:8000; done
"""
import uuid

import fastapi

import sky

app = fastapi.FastAPI()


def background_task(task_id: int):
    task = sky.Task(run='echo hi').set_resources(
        sky.Resources(cpus=2, use_spot=True))
    sky.launch(
        task,
        cluster_name=f'sky-{task_id}',
        down=True,
        idle_minutes_to_autostop=1,
        # Use these to avoid tailing logs:
        detach_setup=True,
        detach_run=True,
    )


@app.get('/')
async def root(background_tasks: fastapi.BackgroundTasks):
    task_id = str(uuid.uuid4())[-6:]
    background_tasks.add_task(background_task, task_id)
    return {'message': f'Background task `sky launch` started: {task_id}'}


def main():
    import uvicorn
    uvicorn.run(app, host='0.0.0.0', port=8000)


if __name__ == '__main__':
    main()

Copy link
Contributor

This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 10 days.

@github-actions github-actions bot added the Stale label Nov 12, 2023
@Michaelvll
Copy link
Collaborator

This is being added in #2735

@Michaelvll Michaelvll removed the Stale label Nov 12, 2023

This comment was marked as outdated.

Copy link
Contributor

This issue is stale because it has been open 120 days with no activity. Remove stale label or comment or this will be closed in 10 days.

@github-actions github-actions bot added the Stale label Jul 11, 2024
Copy link
Contributor

This issue was closed because it has been stalled for 10 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
friction-log good first issue Good for newcomers help wanted Extra attention is needed Stale
Projects
None yet
Development

No branches or pull requests

2 participants