spawn f
spawns a process running f
, supplying f
a hub that it may use to communicate with other processes. f
should listen to the hub to receive messages from the clients. spawn
returns a channel connected to f
's hub, and a deferred that will become determined if f
returns.
There is no guarantee that the deferred returned by this function will become determined before the spawned process runs, as such the following code is a race, and may never return.
| spawn (fun hub -> Hub.send_to_all hub `Hello; Deferred.never ()) | >>= fun (channel, _) -> | Channel.read channel | >>= fun `Hello -> | ...
It IS however guaranteed that the spawned process is listening when the deferred returned by this function is returned, it is theirfore recommended that the spawning process initiate the first communication.
If where
is specified, it controls which machine the process is spawned on. The default is the local machine. You must have passed a list of machines to init in order to use `On, or `Random_on. An exception will be raised if you try to use a machine you didn't pass to init.
The closure you pass may not contain custom blocks with unimplemented serialization functions or Abstract values. Anything you can't pass to Marshal, you can't pass to spawn.