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 async fn init(&mut self) -> Result<(), ExitReason> {
75 let server = Process::current();
76
77 Process::spawn(async move {
78 // Ask for a formatted string.
79 let hello_world = MyServer::hello(server, "hello")
80 .await
81 .expect("Failed to call server!");
82
83 tracing::info!("Got: {:?}", hello_world);
84
85 // Wait before crashing.
86 Process::sleep(Duration::from_secs(1)).await;
87
88 // Crash the process so the supervisor restarts it.
89 MyServer::cast(server, MyMessage::Crash);
90 });
91
92 Ok(())
93 }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 async fn init(&mut self) -> Result<(), ExitReason> {
75 let server = Process::current();
76
77 Process::spawn(async move {
78 // Ask for a formatted string.
79 let hello_world = MyServer::hello(server, "hello")
80 .await
81 .expect("Failed to call server!");
82
83 tracing::info!("Got: {:?}", hello_world);
84
85 // Wait before crashing.
86 Process::sleep(Duration::from_secs(1)).await;
87
88 // Crash the process so the supervisor restarts it.
89 MyServer::cast(server, MyMessage::Crash);
90 });
91
92 Ok(())
93 }More examples
58 async fn start(&self) -> Result<Pid, ExitReason> {
59 // Spawn a registry that will take care of registering 'MySpace'.
60 let children = [
61 Registry::new("space-registry")
62 .with_start(|key| {
63 let RegistryKey::String(id) = key else {
64 panic!()
65 };
66
67 MySpace::new(id).start_link(GenServerOptions::new())
68 })
69 .with_shutdown(Shutdown::Infinity)
70 .child_spec(RegistryOptions::new())
71 .id("space-registry"),
72 ChildSpec::new("test-registry")
73 .start(move || async { Ok(Process::spawn(test_registry())) }),
74 ];
75
76 // Restart only the terminated child.
77 Supervisor::with_children(children)
78 .strategy(SupervisionStrategy::OneForOne)
79 .start_link(SupervisorOptions::new())
80 .await
81 }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 async fn init(&mut self) -> Result<(), ExitReason> {
75 let server = Process::current();
76
77 Process::spawn(async move {
78 // Ask for a formatted string.
79 let hello_world = MyServer::hello(server, "hello")
80 .await
81 .expect("Failed to call server!");
82
83 tracing::info!("Got: {:?}", hello_world);
84
85 // Wait before crashing.
86 Process::sleep(Duration::from_secs(1)).await;
87
88 // Crash the process so the supervisor restarts it.
89 MyServer::cast(server, MyMessage::Crash);
90 });
91
92 Ok(())
93 }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.
Sourcepub fn exit<E: Into<ExitReason>>(pid: Pid, exit_reason: E)
pub fn exit<E: Into<ExitReason>>(pid: Pid, exit_reason: E)
Sends an exit signal with the given reason to Pid.
Sourcepub fn info(pid: Pid) -> Option<ProcessInfo>
pub fn info(pid: Pid) -> Option<ProcessInfo>
Fetches debug information for a given local process.