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.

Using a single session with FastAPI

See original GitHub issue

I have a Hasura GraphQL engine as a server with a few small services acting like webhooks for business logic and handling database events. One of theses services has a REST API and needs to retrieve data from the GraphQL engine or run mutations. Due to performance concerns I have decided to rewrite one of the services with FastAPI in order to leverage async.

I am quite new to async in Python in general which is why I took my time to go through gql documentation. It is my understanding that it is ideal to keep a single async client session throughout the life span of the service. It is also my understanding that the only way of getting an async session is using the async with client as session: syntax.

That poses the question of how can I wrap the whole app inside of the async with. Or perhaps I am missing something and there is a better way of doing this.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:11

github_iconTop GitHub Comments

3reactions
leszekhanuszcommented, Dec 10, 2021

This should be made easier.

After the stable version has been done, I propose to add a new interface to create permanent sessions:

permanent_session = client.permanentSession()

await permanent_session.connect()

# Add your queries and subscriptions here, the permanent_session can be
# kept in a class and will automatically reconnect if needed

await permanent_session.close()
1reaction
motybzcommented, Jun 22, 2022

Hi @leszekhanusz, thank you for your awesome work.

I tested (stress test) it with my FastAPI project and it solve all of the session handling Thank you very much!

I’m looking forward to the official release

(I used https://locust.io/ for the stress test)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Session with FastAPI Dependency - SQLModel
A FastAPI dependency is very simple, it's just a function that returns a value. It could use yield instead of return , and...
Read more >
FastAPI Sessions
Quickly add session authentication to your FastAPI project. FastAPI Sessions is designed to be user friendly and customizable.
Read more >
How to create a session using Fastapi so when a user login ...
How to create a session using Fastapi so when a user login we can use it's id for multipurpose. I mean we can...
Read more >
FastAPI ECommerce tutorial_ FastAPI Session - YouTube
This video show what is session and How to implement session in FastAPI. Want to learn more?
Read more >
fastapi-sessions - PyPI
Ready-to-use session library for FastAPI. ... Abstract session backend so you can build one that fits your needs; Abstract frontends to choose how...
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