Process
A lightweight concurrent process backed by a Go goroutine.
Processes are created by sending fork to a block, which evaluates
the block in a new goroutine and returns a Process handle. Use wait
to block until the process completes and retrieve its result.
[42] fork wait "=> 42"
[3 + 4] fork wait "=> 7"
| p <Process> |
p := [100] fork.
p wait.
p isDone "=> true"
"Fan-out with channels"
| ch <Channel> |
ch := Channel new: 10.
1 to: 5 do: [:i |
[ch send: i * 10] fork
].
5 timesRepeat: [ch receive]
Class Methods
primitives
Return the current process.
Fork block as a new process.
Fork block with restricted global access.
Suspend the current process for ms milliseconds.
Yield the current process's time slice.
uncategorized
Look up a process by its registered name.
Returns the Process or nil if not found.
| p <Process> |
p := [Process receive] fork.
Process sleep: 10.
p registerAs: 'lookup-test'.
Process named: 'lookup-test'
Block the current process until a message arrives in its mailbox. Returns a MailboxMessage.
| p <Process> |
p := [Process receive payload] fork.
Process sleep: 10.
p send: 42.
p wait
Block the current process for up to ms milliseconds
waiting for a message. Returns a MailboxMessage or nil
on timeout.
| p <Process> |
p := [Process receive: 50] fork.
p wait
Non-blocking receive. Returns a MailboxMessage if one is
available, or nil if the mailbox is empty.
Process tryReceive >>> nil
Process current send: 42.
Process tryReceive class name >>> #MailboxMessage
Instance Methods
primitives
Return true if the process has finished.
uncategorized
Cancel a monitor by its reference ID.
| worker <Process> ref <Integer> |
worker := [Process sleep: 5000] fork.
ref := Process current monitor: worker.
Process current demonitor: ref
Return the exit reason of this process (a Symbol like
#normal, #linked, #error), or nil if still running.
Process current exitReason >>> nil
Return true if the process is still running.
| p <Process> |
p := [1] fork.
p wait.
p isAlive "=> false"
Return true if the process has finished executing.
Opposite of isAlive.
| p <Process> |
p := [1] fork.
p wait.
p isTerminated "=> true"
Create a bidirectional link with another process. If either linked process terminates abnormally, the other is also killed (unless it is trapping exits).
| child <Process> |
child := [Process sleep: 1000] fork.
Process current link: child
Monitor another process. Returns a reference ID (SmallInt).
When the monitored process dies, a processDown: message
is delivered to this process's mailbox with payload
#(refID processValue reasonSymbol resultValue).
Works with both local and remote processes. For remote
processes, a MonitorProcess RPC is sent to the remote node,
and a NodeHealthMonitor heartbeat loop detects node failures.
| worker <Process> ref <Integer> msg <MailboxMessage> |
worker := [42] fork.
ref := Process current monitor: worker.
worker wait.
Process sleep: 10.
msg := Process tryReceive.
msg selector "=> #processDown:"
Return a string representation of the process.
Return the scheduling priority of this process.
| p <Process> |
p := [1] fork.
p priority
Set the scheduling priority of this process.
| p <Process> |
p := [Process sleep: 100] fork.
p priority: 2
Register this process under a name for lookup by other
processes. Returns true if successful, false if the
name is already taken by a live process.
| p <Process> |
p := [Process receive] fork.
Process sleep: 10.
p registerAs: 'worker'
Return the result of the process, or nil if it has not
yet completed.
| p <Process> |
p := ['hello'] fork.
p wait.
p result "=> 'hello'"
Send a value to this process's mailbox (fire-and-forget).
Returns true if the message was delivered, false if the
process is dead or its mailbox is full.
| p <Process> |
p := [Process receive] fork.
Process sleep: 10.
p send: 42
Send a selector-based message to this process's mailbox. The receiver gets a MailboxMessage with the given selector and payload.
| p <Process> |
p := [Process receive] fork.
Process sleep: 10.
p send: #doWork with: 42
Terminate the process. The process will stop executing and its result will be nil.
| p <Process> |
p := [Process sleep: 10000] fork.
p terminate
Return whether this process is trapping exits.
Process current trapExit: true.
Process current trapExit >>> true
Set whether this process traps exit signals from linked
processes. When true, exit signals are delivered as mailbox
messages with selector #exit instead of killing the process.
Process current trapExit: true >>> true
Remove a bidirectional link.
Remove this process's name registration.
Block the caller until this process finishes, then return the process's result value.
[42] fork wait "=> 42"
Yield the current process's time slice to allow other goroutines to run. Returns the receiver.
100 timesRepeat: [Process yield]