[#501] handle sync when uvloop.Loop is running
Summary of change
(A few sentences about this PR)
Related issues
- #501
Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos!)
Documentation changes
(If relevant, please create a PR in our docs repo, or create a checklist here highlighting the necessary changes)
Checklist for important updates
- [x] Changelog has been updated
- [ ]
coreDriverInterfaceSupported.jsonfile has been updated (if needed)- Along with the associated array in
supertokens_python/constants.py
- Along with the associated array in
- [ ]
frontendDriverInterfaceSupported.jsonfile has been updated (if needed) - [ ] Changes to the version if needed
- In
setup.py - In
supertokens_python/constants.py
- In
- [ ] Had installed and ran the pre-commit hook
- [ ] Issue this PR against the latest non released version branch.
- To know which one it is, run find the latest released tag (
git tag) in the formatvX.Y.Z, and then find the latest branch (git branch --all) whoseX.Yis greater than the latest released tag. - If no such branch exists, then create one from the latest released branch.
- To know which one it is, run find the latest released tag (
- [ ] If have added a new web framework, update the
supertokens_python/utils.pyfile to include that in theFRAMEWORKSvariable - [ ] If added a new recipe that has a User type with extra info, then be sure to change the User type in supertokens_python/types.py
- [ ] Make sure that
syncio/asynciofunctions are consistent. - [ ] If access token structure has changed
- Modified test in
tests/sessions/test_access_token_version.pyto account for any new claims that are optional or omitted by the core
- Modified test in
Remaining TODOs for this PR
- [ ] Item1
- [ ] Item2
Thanks @virajkanwade , this PR needs to add tests:
- Unit tests
- update the existing e2e test server to test this change, or create a new e2e test server that runs fastapi in this mode.
- Pre commit hook PR check is failing, so that needs fixing too.
One question: In our e2e test, we already run fastapi server using uvicorn app:app, and it works fine (see here). So i wonder why you ran into an issue?
One question: In our e2e test, we already run fastapi server using
uvicorn app:app, and it works fine (see here). So i wonder why you ran into an issue?
something very specific to how I am setting it up
def create_app():
# $ export SUPERTOKENS_NEST_ASYNCIO=1
sync(supertokens_init())
sync(supertokens_multitenancy_setup_personal())
sync(supertokens_multitenancy_setup_school())
app = FastAPI()
app.add_middleware(get_middleware())
where the multitenancy setup functions call create_or_update_tenant and create_or_update_third_party_config.
What are the import statements that you have used for the functions from our lib?
What are the import statements that you have used for the functions from our lib?
I had tried
from supertokens_python.recipe.multitenancy.syncio import (
create_or_update_tenant,
create_or_update_third_party_config,
)
Right. So why do you need to wrap them with sync()?
Right. So why do you need to wrap them with
sync()?
The syncio functions gave the "loop already running" error.
So I followed the code to see the difference between syncio and asyncio functions, and found supertokens sync function.
Then did the whol research above and created my own sync function, hence the sync calls you are seeing in the snippet.
One question: In our e2e test, we already run fastapi server using
uvicorn app:app, and it works fine (see here). So i wonder why you ran into an issue?
@rishabhpoddar If I run it as
uvicorn --factory main:create_app
I get
RuntimeError: this event loop is already running.
If I run it as
if __name__ == '__main__':
import uvicorn
uvicorn.run(
create_app(),
)
python main.py
it seems to work with the supertokens syncio functions
I see. Okay! Thanks for the info. We can continue working on this PR, in a few weeks, to add more tests (as described in my previous comment).
Not merging this. The right way to use it is described in this issue - https://github.com/supertokens/supertokens-python/issues/501