Skip to content

Commit

Permalink
ENH: Move higher level request to run in separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mgxd committed Oct 18, 2024
1 parent 7d59833 commit fca9ba2
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion migas/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Optional, Tuple, Union
from http.client import HTTPConnection, HTTPResponse, HTTPSConnection
from urllib.parse import urlparse
from concurrent.futures import ThreadPoolExecutor

from . import __version__
from .config import logger
Expand All @@ -20,6 +21,32 @@


def request(
url: str,
*,
query: str = None,
timeout: float = None,
method: str = "POST",
chunk_size: int | None = None,
wait: bool = False,
) -> None:
"""
Send a non-blocking call to the server.
This will never check the future, and no assumptions can be made about server receptivity.
"""
with ThreadPoolExecutor() as executor:
future = executor.submit(
_request, url, query=query, timeout=timeout, method=method, chunk_size=chunk_size,
)

if wait is True:
print('Waiting for result!')
return future.result()
else:
print('No wait!')


def _request(
url: str,
*,
query: str = None,
Expand Down Expand Up @@ -73,7 +100,7 @@ def request(
finally:
conn.close()

if body and response.headers.get("content-type").startswith("application/json"):
if body and response.headers.get("content-type", "").startswith("application/json"):
body = json.loads(body)

if not response.headers.get("X-Backend-Server"):
Expand Down

0 comments on commit fca9ba2

Please sign in to comment.