This guide provides examples, tutorials, notes and other documentation that do not belong in The Neuron Reference.
Neuron is a series of Racket libraries that provide a consistent API over a spectrum of functionality related to the creation, operation, integration, and evolution of concurrent, distributed, and decentralized run time environments and applications. At its core is a communication-based concurrency model and a structural pattern-based DSL for working with composable evaluators.
Neuron uses a concurrency model of lightweight processes communicating over first-class named synchronous channels. Processes extend threads with support for life cycle hooks and two orthogonal lines of communication. In other words, a process is like a thread that can clean up after itself and keep “secrets.”
In the future, a paused state and an on-pause hook might be added.
A process is created in the starting state when another process attempts to spawn a new thread of execution. The requesting process blocks until the new process is alive and a fresh process descriptor for it has been returned.
A process stays alive until its thread of execution terminates. A process can terminate itself, either by reaching the end of its program or by issuing a quit or die command. A process can also use the stop or kill command to terminate any process it holds a process descriptor for.
When a process is terminated by quit or stop, it enters the stopping state while it calls its on-stop hook. When a process reaches the end of its program or on-stop hook, or is terminated by a die or kill command, it enters the dying state while it calls its on-dead hook. A process is dead when its on-dead hook returns.
The on-dead hook is for freeing resources no longer needed by any process. Neuron uses the on-dead hook internally to terminate network listeners and kill sub-processes. This hook runs unconditionally and can’t be canceled.
The on-stop hook is for extra or optional clean-up tasks. Neuron uses
the on-stop hook to close ports, terminate network connections,
and stop sub-processes. For example, a codec closes its
input port and output port when stopped—
The deadlock function waits for the current process to terminate, allowing the computation to diverge efficiently. It can be used as a termination “latch” to prevent the current process from ending until stopped or killed.
Applying a process descriptor to an argument list invokes its command handler, a simple dispatch mechanism. Because the command handler is installed while a process is starting, it can have direct access to the internal state of the process via the constructing closure.
Neuron uses the command handler to provide simple properties and methods.
Processes can also communicate by exchanging values through their input channel and output channel. Most of the process constructors provided by Neuron are designed for channel-based data flow networking, hence the distinction between “in-band” channel-mediated exchanges versus “out-of-band” command invocations.
Processes can also be combined in non-trivial ways to provide restricted or revocable access to others.
> (define π (sexp-codec (string-socket #:in "12 34 56" #:out #t))) > (define to-π (proxy-to π)) > (define from-π (proxy-from π)) > (recv from-π)
> (give to-π 'abc)
> (get-output-string (π 'socket))
> (sync/timeout 0 (recv-evt to-π))
> (sync/timeout 0 (give-evt from-π))
> (define π (sexp-codec (string-socket #:in "12 34 56"))) > (define π-ref (proxy π)) > (recv π-ref)
> (kill π-ref) ; after sharing π-ref > (recv π-ref) ; no longer works
> (recv π) ; but π still does