Skip to content

Commit 7009b19

Browse files
committed
fix: Replace asyncio.to_thread with Python 3.8 compatible code
Use asyncio.get_event_loop().run_in_executor() instead of asyncio.to_thread() for Python 3.8 compatibility. Added TODO comments to switch back when Python 3.8 support is dropped.
1 parent 9c4cceb commit 7009b19

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/functions_framework/aio/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ async def handler(request):
107107
if is_async:
108108
result = await function(request)
109109
else:
110-
result = await asyncio.to_thread(function, request)
110+
# TODO: Use asyncio.to_thread when we drop Python 3.8 support
111+
# Python 3.8 compatible version of asyncio.to_thread
112+
loop = asyncio.get_event_loop()
113+
result = await loop.run_in_executor(None, function, request)
111114
if isinstance(result, str):
112115
return Response(result)
113116
elif isinstance(result, dict):
@@ -138,7 +141,10 @@ async def handler(request):
138141
if is_async:
139142
await function(event)
140143
else:
141-
await asyncio.to_thread(function, event)
144+
# TODO: Use asyncio.to_thread when we drop Python 3.8 support
145+
# Python 3.8 compatible version of asyncio.to_thread
146+
loop = asyncio.get_event_loop()
147+
await loop.run_in_executor(None, function, event)
142148
return Response("OK")
143149

144150
return handler

0 commit comments

Comments
 (0)