Skip to content

Commit c711012

Browse files
async fix for sync requests call in abstraction
1 parent 16e02a5 commit c711012

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

Framework/Utilities/RequestFormatter.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -- coding: utf-8 --
22
# -- coding: cp1252 --
3-
3+
import asyncio
44
from . import ConfigModule
55
import os
66
import requests
@@ -182,6 +182,14 @@ def request(*args, **kwargs):
182182

183183
return session.request(*args, **kwargs)
184184

185+
# async wrapper
186+
async def async_request(*args, **kwargs):
187+
"""
188+
Runs the blocking request() in a worker thread
189+
so the event loop is not blocked.
190+
"""
191+
return await asyncio.to_thread(request, *args, **kwargs)
192+
185193

186194
def Post(resource_path, payload=None, **kwargs):
187195
renew_token_with_expiry_check()

Framework/deploy_handler/long_poll_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ async def run(self, host: str) -> None:
265265

266266
try:
267267
reconnect = True
268-
resp = RequestFormatter.request("get", host, timeout=70)
268+
resp = await RequestFormatter.async_request("get", host, timeout=70)
269269
if resp is None:
270270
break
271271

Framework/install_handler/long_poll_handler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ async def run(self) -> None:
339339
f"d/nodes/install/node/listen?node_id={read_node_id()}"
340340
)
341341

342-
resp = resp = RequestFormatter.request("get", host, timeout=70)
342+
resp = await RequestFormatter.async_request("get", host, timeout=70)
343343

344344
if not resp.ok:
345345
if debug:
@@ -368,5 +368,5 @@ async def run(self) -> None:
368368
print("[installer] RETRYING...")
369369
await asyncio.sleep(random.randint(1, 3))
370370

371-
self.running = False
372-
print("[installer] Stopped running")
371+
self.running = False
372+
print("[installer] Stopped running")

Framework/install_handler/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def send_response(data=None) -> None:
6060

6161
for _ in range(3):
6262
try:
63-
resp = RequestFormatter.request("post", host, json=data, timeout=70)
63+
resp = await RequestFormatter.request("post", host, json=data, timeout=70)
6464
if debug:
6565
print(f"[installer] Response status: {resp.status_code}")
6666
print(f"[installer] Response content: {resp.content}")

0 commit comments

Comments
 (0)