pub struct Process { /* private fields */ }Expand description
A light weight task that can send and receive messages.
Implementations§
source§impl Process
impl Process
sourcepub fn alias(reply: bool) -> Reference
pub fn alias(reply: bool) -> Reference
Creates a process alias. If reply is true the alias deactivates when the first message is received.
Otherwise, you need to call unalias(reference) to deactivate the alias.
sourcepub fn unalias(alias: Reference) -> bool
pub fn unalias(alias: Reference) -> bool
Explicitly deactivates a process alias.
Returns true if the alias was currently-active for the current process, or false otherwise.
sourcepub fn current() -> Pid
pub fn current() -> Pid
Returns the current Pid.
Examples found in repository?
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
async fn init(&mut self) -> Result<(), ExitReason> {
let server = Process::current();
Process::spawn(async move {
// Ask for a formatted string.
let hello_world = MyServer::hello(server, "hello")
.await
.expect("Failed to call server!");
tracing::info!("Got: {:?}", hello_world);
// Wait before crashing.
Process::sleep(Duration::from_secs(1)).await;
// Crash the process so the supervisor restarts it.
MyServer::cast(server, MyMessage::Crash);
});
Ok(())
}sourcepub fn send<D: Into<Dests>, M: Receivable>(dests: D, message: M)
pub fn send<D: Into<Dests>, M: Receivable>(dests: D, message: M)
Sends a message to dests.
§Example:
This method allows sending to multiple targets with certain trade offs:
let pid1 = Process::spawn(async { /* */ });
let pid2 = Process::spawn(async { /* */ });
// faster when all processes are local.
for pid in &[pid1, pid2] {
Process::send(pid, "hello world!");
}
// faster when processes are mostly remote.
Process::send(&[pid1, pid2], "hello world!");sourcepub fn send_after<D: Into<Dests>, M: Receivable>(
dest: D,
message: M,
duration: Duration
) -> Reference
pub fn send_after<D: Into<Dests>, M: Receivable>( dest: D, message: M, duration: Duration ) -> Reference
Sends a message to dests after the given duration.
See Process::send for performance trade-offs.
§Example:
Sends a message after 5 seconds to pid:
Process::send_after(pid, "hello world!", Duration::from_secs(5));sourcepub fn cancel_timer(timer: Reference)
pub fn cancel_timer(timer: Reference)
Cancels a timer created by send_after.
sourcepub fn read_timer(timer: Reference) -> Option<Duration>
pub fn read_timer(timer: Reference) -> Option<Duration>
Reads a timer created by send_after.
It returns the time remaining until the timer expires.
sourcepub fn receiver() -> ProcessReceiver<()>
pub fn receiver() -> ProcessReceiver<()>
Creates a receiver with advanced filtering options from the current processes mailbox.
sourcepub async fn receive<T: Receivable>() -> Message<T>
pub async fn receive<T: Receivable>() -> Message<T>
Creates a receiver for a single message that matches the given type from the current processes mailbox.
This will panic if a message is received that doesn’t match the given type.
sourcepub fn spawn<T>(function: T) -> Pid
pub fn spawn<T>(function: T) -> Pid
Spawns the given function as a process and returns it’s Pid.
Examples found in repository?
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
async fn init(&mut self) -> Result<(), ExitReason> {
let server = Process::current();
Process::spawn(async move {
// Ask for a formatted string.
let hello_world = MyServer::hello(server, "hello")
.await
.expect("Failed to call server!");
tracing::info!("Got: {:?}", hello_world);
// Wait before crashing.
Process::sleep(Duration::from_secs(1)).await;
// Crash the process so the supervisor restarts it.
MyServer::cast(server, MyMessage::Crash);
});
Ok(())
}More examples
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
async fn start(&self) -> Result<Pid, ExitReason> {
// Spawn a registry that will take care of registering 'MySpace'.
let children = [
Registry::new("space-registry")
.with_start(|key| {
let RegistryKey::String(id) = key else {
panic!()
};
MySpace::new(id).start_link(GenServerOptions::new())
})
.with_shutdown(Shutdown::Infinity)
.child_spec(RegistryOptions::new())
.id("space-registry"),
ChildSpec::new("test-registry")
.start(move || async { Ok(Process::spawn(test_registry())) }),
];
// Restart only the terminated child.
Supervisor::with_children(children)
.strategy(SupervisionStrategy::OneForOne)
.start_link(SupervisorOptions::new())
.await
}sourcepub fn spawn_link<T>(function: T) -> Pid
pub fn spawn_link<T>(function: T) -> Pid
Spawns the given function as a process, creates a link between the calling process, and returns the new Pid.
sourcepub fn spawn_monitor<T>(function: T) -> (Pid, Reference)
pub fn spawn_monitor<T>(function: T) -> (Pid, Reference)
Spawns the given function as a process, creates a monitor for the calling process, and returns the new Pid.
sourcepub async fn sleep(duration: Duration)
pub async fn sleep(duration: Duration)
Sleeps the current process for the given duration.
Examples found in repository?
More examples
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
async fn init(&mut self) -> Result<(), ExitReason> {
let server = Process::current();
Process::spawn(async move {
// Ask for a formatted string.
let hello_world = MyServer::hello(server, "hello")
.await
.expect("Failed to call server!");
tracing::info!("Got: {:?}", hello_world);
// Wait before crashing.
Process::sleep(Duration::from_secs(1)).await;
// Crash the process so the supervisor restarts it.
MyServer::cast(server, MyMessage::Crash);
});
Ok(())
}sourcepub async fn timeout<F>(
duration: Duration,
future: F
) -> Result<<F as Future>::Output, Timeout>where
F: Future,
pub async fn timeout<F>(
duration: Duration,
future: F
) -> Result<<F as Future>::Output, Timeout>where
F: Future,
Waits for the given future to complete until the given duration is up.
sourcepub fn register<S: Into<String>>(pid: Pid, name: S) -> Result<(), ArgumentError>
pub fn register<S: Into<String>>(pid: Pid, name: S) -> Result<(), ArgumentError>
Registers the given Pid under name if the process is local, active, and the name is not already registered.
sourcepub fn unregister<S: AsRef<str>>(name: S)
pub fn unregister<S: AsRef<str>>(name: S)
Removes the registered name, associated with a Pid.
sourcepub fn registered() -> Vec<String>
pub fn registered() -> Vec<String>
Returns a Vec of registered process names.
sourcepub fn link(pid: Pid)
pub fn link(pid: Pid)
Creates a bi-directional link between the current process and the given process.
sourcepub fn monitor<T: Into<Dest>>(process: T) -> Reference
pub fn monitor<T: Into<Dest>>(process: T) -> Reference
Starts monitoring the given process from the calling process. If the process is already dead a message is sent immediately.
sourcepub fn monitor_alias<T: Into<Dest>>(process: T, reply: bool) -> Reference
pub fn monitor_alias<T: Into<Dest>>(process: T, reply: bool) -> Reference
Starts monitoring the given process from the calling process. If the process is already dead a message is sent immediately.
Creates an alias for the calling process that’s tied to the process monitor.
The alias will be deactivated if:
- The monitor sends a down message.
- The user explicitly calls
unalias. (The monitor will remain active) replyistrueand a message is sent over the alias.
sourcepub fn demonitor(monitor: Reference)
pub fn demonitor(monitor: Reference)
Demonitors the monitor identified by the given reference.
If a monitor message was sent to the process already but was not received, it will be discarded automatically.
sourcepub fn flags() -> ProcessFlags
pub fn flags() -> ProcessFlags
Returns the current process flags.
sourcepub fn set_flags(flags: ProcessFlags)
pub fn set_flags(flags: ProcessFlags)
Sets one or more process flags.