@@ -42,22 +42,34 @@ <h1 class="title">Module <code>supertokens_python.async_to_sync_wrapper</code></
4242
4343import asyncio
4444from typing import Any, Coroutine, TypeVar
45+ from os import getenv
4546
4647_T = TypeVar("_T")
4748
4849
49- def check_event_loop():
50+ def nest_asyncio_enabled():
51+ return getenv("SUPERTOKENS_NEST_ASYNCIO", "") == "1"
52+
53+
54+ def create_or_get_event_loop() -> asyncio.AbstractEventLoop:
5055 try:
51- asyncio.get_event_loop()
52- except RuntimeError as ex:
56+ return asyncio.get_event_loop()
57+ except Exception as ex:
5358 if "There is no current event loop in thread" in str(ex):
5459 loop = asyncio.new_event_loop()
60+
61+ if nest_asyncio_enabled():
62+ import nest_asyncio # type: ignore
63+
64+ nest_asyncio.apply(loop) # type: ignore
65+
5566 asyncio.set_event_loop(loop)
67+ return loop
68+ raise ex
5669
5770
5871def sync(co: Coroutine[Any, Any, _T]) -> _T:
59- check_event_loop()
60- loop = asyncio.get_event_loop()
72+ loop = create_or_get_event_loop()
6173 return loop.run_until_complete(co)</ code > </ pre >
6274</ details >
6375</ section >
@@ -68,22 +80,43 @@ <h1 class="title">Module <code>supertokens_python.async_to_sync_wrapper</code></
6880< section >
6981< h2 class ="section-title " id ="header-functions "> Functions</ h2 >
7082< dl >
71- < dt id ="supertokens_python.async_to_sync_wrapper.check_event_loop "> < code class ="name flex ">
72- < span > def < span class ="ident "> check_event_loop </ span > </ span > (< span > )</ span >
83+ < dt id ="supertokens_python.async_to_sync_wrapper.create_or_get_event_loop "> < code class ="name flex ">
84+ < span > def < span class ="ident "> create_or_get_event_loop </ span > </ span > (< span > ) ‑ > asyncio.events.AbstractEventLoop </ span >
7385</ code > </ dt >
7486< dd >
7587< div class ="desc "> </ div >
7688< details class ="source ">
7789< summary >
7890< span > Expand source code</ span >
7991</ summary >
80- < pre > < code class ="python "> def check_event_loop() :
92+ < pre > < code class ="python "> def create_or_get_event_loop() -> asyncio.AbstractEventLoop :
8193 try:
82- asyncio.get_event_loop()
83- except RuntimeError as ex:
94+ return asyncio.get_event_loop()
95+ except Exception as ex:
8496 if "There is no current event loop in thread" in str(ex):
8597 loop = asyncio.new_event_loop()
86- asyncio.set_event_loop(loop)</ code > </ pre >
98+
99+ if nest_asyncio_enabled():
100+ import nest_asyncio # type: ignore
101+
102+ nest_asyncio.apply(loop) # type: ignore
103+
104+ asyncio.set_event_loop(loop)
105+ return loop
106+ raise ex</ code > </ pre >
107+ </ details >
108+ </ dd >
109+ < dt id ="supertokens_python.async_to_sync_wrapper.nest_asyncio_enabled "> < code class ="name flex ">
110+ < span > def < span class ="ident "> nest_asyncio_enabled</ span > </ span > (< span > )</ span >
111+ </ code > </ dt >
112+ < dd >
113+ < div class ="desc "> </ div >
114+ < details class ="source ">
115+ < summary >
116+ < span > Expand source code</ span >
117+ </ summary >
118+ < pre > < code class ="python "> def nest_asyncio_enabled():
119+ return getenv("SUPERTOKENS_NEST_ASYNCIO", "") == "1"</ code > </ pre >
87120</ details >
88121</ dd >
89122< dt id ="supertokens_python.async_to_sync_wrapper.sync "> < code class ="name flex ">
@@ -96,8 +129,7 @@ <h2 class="section-title" id="header-functions">Functions</h2>
96129< span > Expand source code</ span >
97130</ summary >
98131< pre > < code class ="python "> def sync(co: Coroutine[Any, Any, _T]) -> _T:
99- check_event_loop()
100- loop = asyncio.get_event_loop()
132+ loop = create_or_get_event_loop()
101133 return loop.run_until_complete(co)</ code > </ pre >
102134</ details >
103135</ dd >
@@ -119,7 +151,8 @@ <h2>Index</h2>
119151</ li >
120152< li > < h3 > < a href ="#header-functions "> Functions</ a > </ h3 >
121153< ul class ="">
122- < li > < code > < a title ="supertokens_python.async_to_sync_wrapper.check_event_loop " href ="#supertokens_python.async_to_sync_wrapper.check_event_loop "> check_event_loop</ a > </ code > </ li >
154+ < li > < code > < a title ="supertokens_python.async_to_sync_wrapper.create_or_get_event_loop " href ="#supertokens_python.async_to_sync_wrapper.create_or_get_event_loop "> create_or_get_event_loop</ a > </ code > </ li >
155+ < li > < code > < a title ="supertokens_python.async_to_sync_wrapper.nest_asyncio_enabled " href ="#supertokens_python.async_to_sync_wrapper.nest_asyncio_enabled "> nest_asyncio_enabled</ a > </ code > </ li >
123156< li > < code > < a title ="supertokens_python.async_to_sync_wrapper.sync " href ="#supertokens_python.async_to_sync_wrapper.sync "> sync</ a > </ code > </ li >
124157</ ul >
125158</ li >
0 commit comments