How to reload a server when the code change

I’m curious about the workflow you use to reload some code that contains a server. My code looks like:

% Start the server when the code is loaded.
:- initialization(start).

% Start the server on port 8080
start :- http_server(http_dispatch, [port(8081)]).

but if I change something in the functions that implement the handlers, and issue make. at the top-level, I get:

ERROR: [Thread 3] ~/code/prolog/example/server.pl:18: Initialization goal raised exception:
ERROR: [Thread 3] Socket error: Address already in use

What’s the workflow you would suggest here? In other programming languages that do heavy use of the repl, like clojure, we have facilities for closing the long lived resources and starting them again on reload, but can’t find more info on how to do that here.

Use

:- initialization(start, main).

The initialization/2 is an extension to the ISO initialization/1 that allow for specifying when the initialization runs. This also allows for swipl -l myfile.pl which loads the code, but does not run the main initialization goal.

Alternatively, make sure start/0 can run twice or simply ignore the error :slight_smile:

1 Like

Amazing @jan, thank you. initialization(start, main) is an even better solution than the one I had in mind!

When developing low-level code (e.g. C) the usual solution to this is SO_REUSEADDR , see for example

I don’t know if it is possible to do that in SWI Prolog’s socket API.