Skip to main content

A FastAPI-based block hunter application

Project description

🔍 FastAPI Block Hunter

This is a tool to help you debug blocking calls in your FastAPI application.

It works by:

  • 🔄 Adding a middleware to the FastAPI application that will store the current request URL in a context variable.
  • 🛠️ Patching the _format_handle function from the asyncio.base_events module with a custom formatter to print a custom message when a blocking call is detected.

This allows you to identify the blocking call that has blocked the event loop and print the stack trace of the blocking call.

Sample logs showing blocking call detection

📦 Installation

pip install fastapi-block-hunter

🚀 Usage

To use the package, you need to add the middleware to your FastAPI application and set up the asyncio debug mode.

from fastapi import FastAPI
from fastapi_block_hunter import add_block_hunter_middleware, setup_asyncio_debug_mode

# You may set up the asyncio debug mode by defining ASYNgCIO_DEBUG_MODE=1 as an environment variable.
# Or by calling the setup_asyncio_debug_mode function in your app lifespan

@asynccontextmanager
async def lifespan(app: FastAPI):
    await setup_asyncio_debug_mode()
    yield {}


app = FastAPI(lifespan=lifespan)
app = add_block_hunter_middleware(app, verbose=True)

You also need to make sure you use asyncio as your FastAPI event loop for the package to work. If you have uvloop installed, you will need to manually tell your web server to use asyncio instead of uvloop.

For uvicorn, set the --loop option to asyncio.

From the uvicorn command line:

uvicorn --loop asyncio # rest of your startup command

Or from your launch script:

from fastapi import FastAPI
    import uvicorn

app = FastAPI()

# ... your FastAPI app setup

if __name__ == "__main__":
    uvicorn.run(app, loop="asyncio")

Or from the code:

from fastapi import FastAPI
from fastapi_block_hunter import add_custom_slow_callback_logger

app = FastAPI()
app = add_custom_slow_callback_logger(app)

📚 Documentation

A complete example of how to use the package can be found in the example/app.py file.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_block_hunter-0.0.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_block_hunter-0.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_block_hunter-0.0.1.tar.gz.

File metadata

  • Download URL: fastapi_block_hunter-0.0.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for fastapi_block_hunter-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e93a08de335cbfa6be7653d38ffcf1c1672ccc8a5a377e5780974cd0804d14db
MD5 b5d8b50d707782e462867650e36c9954
BLAKE2b-256 3558be9ebc908b6c374569c23193232f003a4a79cda98e9f4ea237fbfa293997

See more details on using hashes here.

File details

Details for the file fastapi_block_hunter-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_block_hunter-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f680613aa1b039179b4cab4d723a37f4620990be53d34866b375b0a3f4b1751e
MD5 73cfcc4528230def9ab79fff0d278269
BLAKE2b-256 8879f2622ae81c0205e4ab8270bb9654fe588c8fbf8ab1c469454f914a1c5a46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page