Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds handling for eventlet/gevent #546

Open
wants to merge 6 commits into
base: 0.25
Choose a base branch
from

Conversation

namsnath
Copy link

@namsnath namsnath commented Nov 23, 2024

Summary of change

  • Adds AsyncHandler (sub)classes to manage async_to_sync across frameworks
  • Adds a new param to Supertokens to configure async type

Examples:

# flask.py

from supertokens_python.async_to_sync.handler import DefaultHandler, AsyncioHandler

init(
    async_handler=DefaultHandler(), # default, no need to set explicitly
    async_handler=AsyncioHandler(), # Similar to DefaultHandler with default params
    async_handler=DefaultHandler(create_loop_thread=True), # Creates a thread to run the event loop
)
# gevent.py

from supertokens_python.async_to_sync.handler import GeventHandler

init(
    async_handler=GeventHandler(), # Creates a thread to run the event loop
)
# eventlet.py

from supertokens_python.async_to_sync.handler import EventletHandler

init(
    async_handler=EventletHandler(), # Creates a thread to run the event loop
)

Optionally, pass in a loop param to use pre-existing loops within Supertokens.

Usage Examples

Python runner

Default
# flask.py
init(
    # ...
)
Existing non-threaded event loop

NOTE: Do NOT use the is_loop_threaded option for non-threaded loops. Will cause the server to not respond.

# flask.py
import asyncio
loop = asyncio.new_event_loop()

init(
    # ...
    # Does not require any additional handling
)
Existing threaded event loop
# flask.py
from supertokens_python.async_to_sync.handler import AsyncioHandler

import asyncio, nest_asyncio, threading
loop = asyncio.new_event_loop()
loop_thread = threading.Thread(target=loop.run_forever, daemon=True)
loop_thread.start()

asyncio.set_event_loop(loop)

init(
    # ...
    async_handler=AsyncioHandler(loop=loop, is_loop_threaded=True)
)
Create new threaded event loop
# flask.py
from supertokens_python.async_to_sync.handler import AsyncioHandler

init(
    # ...
    async_handler=AsyncioHandler(
        create_loop_thread=True,
        is_loop_threaded=True, # Not required, set internally
    )
)

Gevent

Default - Create loop thread for better performance
# gevent.py
from supertokens_python.async_to_sync.handler import GeventHandler

init(
    async_handler=GeventHandler(
        create_loop_thread=True, # Not required, set by default
        is_loop_threaded=True, # Not required, set by default
    ),
)
Existing threaded loop
# gevent.py
from supertokens_python.async_to_sync.handler import GeventHandler

import asyncio, threading
loop = asyncio.new_event_loop()
loop_thread = threading.Thread(target=loop.run_forever, daemon=True)
loop_thread.start()
asyncio.set_event_loop(loop)

init(
    async_handler=GeventHandler(create_loop_thread=False, loop=loop, is_loop_threaded=False),
)

Eventlet

Default - Create loop thread
# eventlet.py
from supertokens_python.async_to_sync.handler import EventletHandler

init(
    async_handler=EventletHandler(
        create_loop_thread=True, # Not required, set by default
        is_loop_threaded=True, # Not required, set by default
    ),
)
Use existing threaded loop
# eventlet.py
from supertokens_python.async_to_sync.handler import EventletHandler

import asyncio, threading
loop = asyncio.new_event_loop()
loop_thread = threading.Thread(target=loop.run_forever, daemon=True)
loop_thread.start()
asyncio.set_event_loop(loop)

init(
    async_handler=EventletHandler(
        create_loop_thread=False,
        loop=loop,
        is_loop_threaded=True,
    ),
)

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

  • Changelog has been updated
  • coreDriverInterfaceSupported.json file has been updated (if needed)
    • Along with the associated array in supertokens_python/constants.py
  • frontendDriverInterfaceSupported.json file has been updated (if needed)
  • Changes to the version if needed
    • In setup.py
    • In supertokens_python/constants.py
  • 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 format vX.Y.Z, and then find the latest branch (git branch --all) whose X.Y is greater than the latest released tag.
    • If no such branch exists, then create one from the latest released branch.
  • If have added a new web framework, update the supertokens_python/utils.py file to include that in the FRAMEWORKS variable
  • 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 / asyncio functions are consistent.
  • If access token structure has changed
    • Modified test in tests/sessions/test_access_token_version.py to account for any new claims that are optional or omitted by the core

Remaining TODOs for this PR

  • Fix cyclic import lint errors (potentially create module for async_to_sync)
  • Optimize the options exposed to users
    • Consider differentiating between loop types (threaded/non-threaded) to change sync handling
  • Add docstrings

- Adds AsyncHandler (sub)classes to manage async_to_sync across frameworks
- Adds a new param to configure async to Supertokens
- Separates logical groups of utils into modules
- Disables pylint cyclic checks on supertokens lazy import
- Adds handling to prevent non-ideal configuration
@namsnath namsnath marked this pull request as ready for review November 24, 2024 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant