@@ -42,22 +42,34 @@ <h1 class="title">Module <code>supertokens_python.async_to_sync_wrapper</code></
42
42
43
43
import asyncio
44
44
from typing import Any, Coroutine, TypeVar
45
+ from os import getenv
45
46
46
47
_T = TypeVar("_T")
47
48
48
49
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:
50
55
try:
51
- asyncio.get_event_loop()
52
- except RuntimeError as ex:
56
+ return asyncio.get_event_loop()
57
+ except Exception as ex:
53
58
if "There is no current event loop in thread" in str(ex):
54
59
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
+
55
66
asyncio.set_event_loop(loop)
67
+ return loop
68
+ raise ex
56
69
57
70
58
71
def sync(co: Coroutine[Any, Any, _T]) -> _T:
59
- check_event_loop()
60
- loop = asyncio.get_event_loop()
72
+ loop = create_or_get_event_loop()
61
73
return loop.run_until_complete(co)</ code > </ pre >
62
74
</ details >
63
75
</ section >
@@ -68,22 +80,43 @@ <h1 class="title">Module <code>supertokens_python.async_to_sync_wrapper</code></
68
80
< section >
69
81
< h2 class ="section-title " id ="header-functions "> Functions</ h2 >
70
82
< 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 >
73
85
</ code > </ dt >
74
86
< dd >
75
87
< div class ="desc "> </ div >
76
88
< details class ="source ">
77
89
< summary >
78
90
< span > Expand source code</ span >
79
91
</ summary >
80
- < pre > < code class ="python "> def check_event_loop() :
92
+ < pre > < code class ="python "> def create_or_get_event_loop() -> asyncio.AbstractEventLoop :
81
93
try:
82
- asyncio.get_event_loop()
83
- except RuntimeError as ex:
94
+ return asyncio.get_event_loop()
95
+ except Exception as ex:
84
96
if "There is no current event loop in thread" in str(ex):
85
97
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 >
87
120
</ details >
88
121
</ dd >
89
122
< 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>
96
129
< span > Expand source code</ span >
97
130
</ summary >
98
131
< 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()
101
133
return loop.run_until_complete(co)</ code > </ pre >
102
134
</ details >
103
135
</ dd >
@@ -119,7 +151,8 @@ <h2>Index</h2>
119
151
</ li >
120
152
< li > < h3 > < a href ="#header-functions "> Functions</ a > </ h3 >
121
153
< 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 >
123
156
< li > < code > < a title ="supertokens_python.async_to_sync_wrapper.sync " href ="#supertokens_python.async_to_sync_wrapper.sync "> sync</ a > </ code > </ li >
124
157
</ ul >
125
158
</ li >
0 commit comments