Skip to content

Commit 3bb7021

Browse files
delthascsernazs
authored andcommitted
Enable processing concurrent requests in separate threads
Closes: csernazs#323
1 parent 0484bd2 commit 3bb7021

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

pytest_httpserver/httpserver.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ class HTTPServerBase(abc.ABC): # pylint: disable=too-many-instance-attributes
598598
:param host: the host or IP where the server will listen
599599
:param port: the TCP port where the server will listen
600600
:param ssl_context: the ssl context object to use for https connections
601+
:param threaded: whether to handle concurrent requests in separate threads
601602
602603
.. py:attribute:: log
603604
@@ -619,6 +620,8 @@ def __init__(
619620
host: str,
620621
port: int,
621622
ssl_context: SSLContext | None = None,
623+
*,
624+
threaded: bool = False,
622625
):
623626
"""
624627
Initializes the instance.
@@ -632,6 +635,7 @@ def __init__(
632635
self.handler_errors: list[Exception] = []
633636
self.log: list[tuple[Request, Response]] = []
634637
self.ssl_context = ssl_context
638+
self.threaded = threaded
635639
self.no_handler_status_code = 500
636640

637641
def __repr__(self):
@@ -730,19 +734,21 @@ def start(self):
730734
This method returns immediately (e.g. does not block), and it's the caller's
731735
responsibility to stop the server (by calling :py:meth:`stop`) when it is no longer needed).
732736
733-
If the sever is not stopped by the caller and execution reaches the end, the
737+
If the server is not stopped by the caller and execution reaches the end, the
734738
program needs to be terminated by Ctrl+C or by signal as it will not terminate until
735739
the thread is stopped.
736740
737-
If the sever is already running :py:class:`HTTPServerError` will be raised. If you are
741+
If the server is already running :py:class:`HTTPServerError` will be raised. If you are
738742
unsure, call :py:meth:`is_running` first.
739743
740744
There's a context interface of this class which stops the server when the context block ends.
741745
"""
742746
if self.is_running():
743747
raise HTTPServerError("Server is already running")
744748

745-
self.server = make_server(self.host, self.port, self.application, ssl_context=self.ssl_context)
749+
self.server = make_server(
750+
self.host, self.port, self.application, ssl_context=self.ssl_context, threaded=self.threaded
751+
)
746752
self.port = self.server.port # Update port (needed if `port` was set to 0)
747753
self.server_thread = threading.Thread(target=self.thread_target)
748754
self.server_thread.start()
@@ -900,6 +906,8 @@ class HTTPServer(HTTPServerBase): # pylint: disable=too-many-instance-attribute
900906
:param default_waiting_settings: the waiting settings object to use as default settings for :py:meth:`wait` context
901907
manager
902908
909+
:param threaded: whether to handle concurrent requests in separate threads
910+
903911
.. py:attribute:: no_handler_status_code
904912
905913
Attribute containing the http status code (int) which will be the response
@@ -916,11 +924,13 @@ def __init__(
916924
port=DEFAULT_LISTEN_PORT,
917925
ssl_context: SSLContext | None = None,
918926
default_waiting_settings: WaitingSettings | None = None,
927+
*,
928+
threaded: bool = False,
919929
):
920930
"""
921931
Initializes the instance.
922932
"""
923-
super().__init__(host, port, ssl_context)
933+
super().__init__(host, port, ssl_context, threaded=threaded)
924934

925935
self.ordered_handlers: list[RequestHandler] = []
926936
self.oneshot_handlers = RequestHandlerList()

0 commit comments

Comments
 (0)