library(http/thread_httpd.pl) provides the
infrastructure to manage multiple clients using a pool of worker-threads.
This realises a popular server design, also seen in Java Tomcat and
Microsoft .NET. As a single persistent server process maintains
communication to all clients startup time is not an important issue and
the server can easily maintain state-information for all clients.
In addition to the functionality provided by the inetd server, the
threaded server can also be used to realise an HTTPS server exploiting
library(ssl) library. See option
- Create the server. Options must provide the
option to specify the port the server should listen to. If Port
is unbound an arbitrary free port is selected and Port is
unified to this port-number. The server consists of a small Prolog
thread accepting new connection on Port and dispatching these
to a pool of workers. Defined Options are:
- Address to bind to. Address is either a port (integer) or a
Host:Port. The port may be a variable, causing the
system to select a free port and unify the variable with the selected
port. See also tcp_bind/2.
- Defines the number of worker threads in the pool. Default is to use
five workers. Choosing the optimal value for best performance
is a difficult task depending on the number of CPUs in your system and
how much resources are required for processing a request. Too high
numbers makes your system switch too often between threads or even swap
if there is not enough memory to keep all threads in memory, while a too
low number causes clients to wait unnecessary for other clients to
complete. See also http_workers/2.
- Determines the maximum period of inactivity handling a request. If no
data arrives within the specified time since the last data arrived, the
connection raises an exception, and the worker discards the client and
returns to the pool-queue for a new client. If it is
a worker may wait forever on a client that doesn't complete its request.
Default is 60 seconds.
- Maximum time to wait for new activity on Keep-Alive
connections. Choosing the correct value for this parameter is hard.
Disabling Keep-Alive is bad for performance if the clients request
multiple documents for a single page. This may ---for example-- be
caused by HTML frames, HTML pages with images, associated CSS files,
etc. Keeping a connection open in the threaded model however prevents
the thread servicing the client servicing other clients. The default is
- Size of the local-stack for the workers. Default is taken from the
- Size of the global-stack for the workers. Default is taken from the
- Size of the trail-stack for the workers. Default is taken from the
- Use SSL (Secure Socket Layer) rather than plain TCP/IP. A server created
this way is accessed using the
https:// protocol. SSL
allows for encrypted communication to avoid others from tapping the wire
as well as improved authentication of client and server. The SSLOptions
option list is passed to ssl_context/3.
The port option of the main option list is forwarded to the SSL layer.
library(ssl) library for details.
- True if Property is a property of the HTTP server running at
Port. Defined properties are:
- Goal used to start the server. This is often http_dispatch/1.
- Scheme is one of
- Time-stamp when the server was created. See format_time/3
for creating a human-readable representation.
- Query or manipulate the number of workers of the server identified by
Port. If Workers is unbound it is unified with the
number of running servers. If it is an integer greater than the current
size of the worker pool new workers are created with the same
specification as the running workers. If the number is less than the
current size of the worker pool, this predicate inserts a number of‘quit'
requests in the queue, discarding the excess workers as they finish
their jobs (i.e. no worker is abandoned while serving a client).
This can be used to tune the number of workers for performance.
Another possible application is to reduce the pool to one worker to
facilitate easier debugging.
- Add a new worker to the HTTP server for port Port. Options
overrule the default queue options. The following additional options are
- The created worker will automatically terminate if there is no new work
- Stop the HTTP server at Port. Halting a server is done
gracefully, which means that requests being processed are not
abandoned. The Options list is for future refinements of this
predicate such as a forced immediate abort of the server, but is
- True if ThreadID is the identifier of a Prolog thread serving
Port. This predicate is motivated to allow for the use of
arbitrary interaction with the worker thread for development and
- Continue handling this request in a new thread running Goal.
the worker returns to the pool to process new requests. In its simplest
form, Spec is the name of a thread pool as defined by
Alternatively it is an option list, whose options are passed to thread_create_in_pool/4
if Spec contains
pool(Pool) or to thread_create/3
of the pool option is not present. If the dispatch module is used (see section
3.2), spawning is normally specified as an option to the http_handler/3
We recomment the use of thread pools. They allow registration of a
set of threads using common characteristics, specify how many can be
active and what to do if all threads are active. A typical application
may define a small pool of threads with large stacks for computation
intensive tasks, and a large pool of threads with small stacks to serve
media. The declaration could be the one below, allowing for max 3
concurrent solvers and a maximum backlog of 5 and 30 tasks creating
:- thread_pool_create(compute, 3,
[ local(20000), global(100000), trail(50000),
:- thread_pool_create(media, 30,
[ local(100), global(100), trail(100),
:- http_handler('/solve', solve, [spawn(compute)]).
:- http_handler('/thumbnail', thumbnail, [spawn(media)]).