supertokens / supertokens-python

Python SDK for SuperTokens
https://supertokens.com
Other
129 stars 38 forks source link

[#501] handle sync when uvloop.Loop is running #502

Closed virajkanwade closed 5 months ago

virajkanwade commented 6 months ago

Summary of change

(A few sentences about this PR)

Related issues

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

Remaining TODOs for this PR

rishabhpoddar commented 6 months ago

Thanks @virajkanwade , this PR needs to add tests:

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?

virajkanwade commented 6 months ago

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.

rishabhpoddar commented 6 months ago

What are the import statements that you have used for the functions from our lib?

virajkanwade commented 6 months ago

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,
)
rishabhpoddar commented 6 months ago

Right. So why do you need to wrap them with sync()?

virajkanwade commented 6 months ago

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.

virajkanwade commented 6 months ago

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

rishabhpoddar commented 6 months ago

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).

sattvikc commented 5 months ago

Not merging this. The right way to use it is described in this issue - https://github.com/supertokens/supertokens-python/issues/501