From fca9ba28cc6638cd948f4d616b9ec6a2570cd034 Mon Sep 17 00:00:00 2001 From: mathiasg Date: Fri, 18 Oct 2024 18:34:09 -0400 Subject: [PATCH] ENH: Move higher level request to run in separate thread --- migas/request.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/migas/request.py b/migas/request.py index eb4db70..444b7a3 100644 --- a/migas/request.py +++ b/migas/request.py @@ -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 @@ -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, @@ -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"):