Hi eveyone,
I try to use RayServe with FastAPI but it didn’t work. This code work well like normal FastAPI app.
app = FastAPI()
app.include_router(user_access_router, prefix='/user/access', tags=['user'])
if __name__=="__main__":
import uvicorn
uvicorn.run("app:app",reload=True)
But this not :
app = FastAPI()
app.include_router(user_access_router, prefix='/user/access', tags=['user'])
@serve.deployment(name='datastore_app')
@serve.ingress(app)
class DatastoreApp:
def __init__(self):
pass
datastore_app = DatastoreApp.bind()
serve.run(datastore_app)
The summary of error log like this:
TypeError: cannot pickle '_thread.lock' object
...
@serve.ingress(app)
^^^^^^^^^^^^^^^^^^
TypeError: Failed to serialize the FastAPI app.
...
=======================================
!!! FAIL serialization: cannot pickle '_thread.lock' object
Serializing 'add_api_route' <bound method FastAPI.add_api_route of <fastapi.applications.FastAPI object at 0x7bdb259e12b0>>...
!!! FAIL serialization: cannot pickle '_thread.lock' object
Serializing '__func__' <function FastAPI.add_api_route at 0x7bdb210b1e40>...
WARNING: Did not find non-serializable object in <bound method FastAPI.add_api_route of <fastapi.applications.FastAPI object at 0x7bdb259e12b0>>. This may be an oversight.
========================================
I inspected with:
inspect_serializability(serve.ingress(app))
The return:
================================================================================
Checking Serializability of <function ingress.<locals>.decorator at 0x7a16395b79c0>
================================================================================
!!! FAIL serialization: cannot pickle '_thread.lock' object
Detected 11 global variables. Checking serializability...
Serializing 'inspect' <module 'inspect' from '/usr/lib/python3.12/inspect.py'>...
Serializing 'collections' <module 'collections' from '/usr/lib/python3.12/collections/__init__.py'>...
Serializing 'Callable' typing.Callable...
Serializing 'FastAPI' <class 'fastapi.applications.FastAPI'>...
Serializing 'APIRouter' <class 'fastapi.routing.APIRouter'>...
Serializing 'make_fastapi_class_based_view' <function make_fastapi_class_based_view at 0x7a163a45ad40>...
Serializing 'ensure_serialization_context' <function ensure_serialization_context at 0x7a163c3c87c0>...
Serializing 'cloudpickle' <module 'ray.cloudpickle' from '/home/hieu/Workspace/projects/chatbone/.venv/lib/python3.12/site-packages/ray/cloudpickle/__init__.py'>...
Serializing 'pickle_dumps' <function pickle_dumps at 0x7a163cb00d60>...
Serializing 'ASGIAppReplicaWrapper' <class 'ray.serve._private.http_util.ASGIAppReplicaWrapper'>...
Serializing '__name__' ray.serve.api...
Detected 1 nonlocal variables. Checking serializability...
Serializing 'app' <fastapi.applications.FastAPI object at 0x7a163f4e4920>...
!!! FAIL serialization: cannot pickle '_thread.lock' object
Serializing 'add_api_route' <bound method FastAPI.add_api_route of <fastapi.applications.FastAPI object at 0x7a163f4e4920>>...
!!! FAIL serialization: cannot pickle '_thread.lock' object
Serializing '__func__' <function FastAPI.add_api_route at 0x7a163aab9da0>...
WARNING: Did not find non-serializable object in <bound method FastAPI.add_api_route of <fastapi.applications.FastAPI object at 0x7a163f4e4920>>. This may be an oversight.
================================================================================
Variable:
FailTuple(add_api_route [obj=<bound method FastAPI.add_api_route of <fastapi.applications.FastAPI object at 0x7a163f4e4920>>, parent=<fastapi.applications.FastAPI object at 0x7a163f4e4920>])
was found to be non-serializable. There may be multiple other undetected variables that were non-serializable.
Consider either removing the instantiation/imports of these variables or moving the instantiation into the scope of the function/class.
================================================================================
I tried to serve the same endpoints fastapi without “include_router” (using only FastAPI() and it still didn’t work. Am i missing something?
Thank you.
UPDATE:
I traced my error and found that it happends because create sqlalchemy.AsyncSession in each FastApi endpoint (both through Dependencies or manual creation give error). But i want to do something like open session (connection), interact with database and close it in each endpoint and i still haven’t found the way to do that with Ray.