首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将fastapi部署到google cloud run

如何将fastapi部署到google cloud run
EN

Stack Overflow用户
提问于 2021-08-04 04:24:42
回答 1查看 1.3K关注 0票数 0

我有一个fastapi应用程序,我想部署到google cloud run上。

对于gRPC python项目,我们将其部署为

代码语言:javascript
运行
复制
gcloud beta run deploy countries --source .

但这并不像预期的那样部署。我看了一个视频,从源代码使用Google cloud buildpacks进行部署。

有没有人有办法做到这一点?

我的代码是这样的

代码语言:javascript
运行
复制
from typing import List
import geopandas as gpd
from fastapi import FastAPI
from geojson_pydantic.geometries import Geometry
from geojson_pydantic.features import Feature
from pydantic import BaseModel
from shapely.geometry.geo import shape
import json
import shapely

class Country(BaseModel):
    name: str
    code: str
    type: str
    geometry: Feature


app = FastAPI(
    title="Natural Earth Countries",
    description="This is a list of endpoints which you can use to access natural earth data.",
    version="0.0.1",
    docs_url='/',
)

gdf = gpd.read_file('data/ne_110m_admin_0_countries.zip')
gdf['name'] = gdf['ADMIN'].apply(lambda x: x.lower())

@app.get('/countries/all')
def get_all_countries() -> List[Country]:
    return rows_to_countries(gdf)


@app.get('/countries/search/{name}')
def search_countries(name: str):
    name = name.lower()
    subset = gdf.loc[gdf["name"].str.contains(name)]
    return rows_to_countries(subset)

@app.get('/countries/within-boundary')
def countries_intersecting(boundary: Geometry):
    bounds = shape(boundary)
    subset = gdf.loc[gdf.intersects(bounds)]
    return rows_to_countries(subset)


def row_to_country(row):
    return Country(
        name=row["ADMIN"],
        type=row["TYPE"],
        geometry=Feature(geometry=row['geometry']),
        code=row["ADM0_A3"],
    )


def rows_to_countries(gdf):
    return [row_to_country(row) for _, row in gdf.iterrows()]

感谢您的帮助

EN

回答 1

Stack Overflow用户

发布于 2021-08-04 11:21:50

我找到的解决方案是创建一个Dockerfile

代码语言:javascript
运行
复制
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8

COPY . /app
ENV APP_HOME /app

WORKDIR $APP_HOME
COPY . ./

RUN pip install -r requirements.txt

CMD exec gunicorn --bind :$PORT --workers 1 --worker-class uvicorn.workers.UvicornWorker  --threads 8 main:app

然后运行

代码语言:javascript
运行
复制
gcloud builds submit --tag gcr.io/PROJECT-ID/countries_fastapi

然后在镜像提交到gcloud后,运行

代码语言:javascript
运行
复制
gcloud run deploy --image gcr.io/bitnami-oyzgng8y5a/countries_fastapi --platform managed
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68645378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档