Skip to content

Commit 3b5716c

Browse files
In-linerobjtede
andauthored
Add getter methods for actix_rt::Runtime and tokio::runtime::Runtime (#484)
Co-authored-by: Alik Aslanyan <[email protected]> Co-authored-by: Rob Ede <[email protected]>
1 parent 0bc310a commit 3b5716c

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

actix-rt/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased - 2023-xx-xx
44

5+
- Add `actix_rt::System::runtime()` method to retrieve the underlying `actix_rt::Runtime` runtime.
6+
- Add `actix_rt::Runtime::tokio_runtime()` method to retrieve the underlying Tokio runtime.
57
- Minimum supported Rust version (MSRV) is now 1.65.
68

79
## 2.8.0 - 2022-12-21

actix-rt/src/runtime.rs

+56
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,62 @@ impl Runtime {
6161
self.local.spawn_local(future)
6262
}
6363

64+
/// Retrieves a reference to the underlying Tokio runtime associated with this instance.
65+
///
66+
/// The Tokio runtime is responsible for executing asynchronous tasks and managing
67+
/// the event loop for an asynchronous Rust program. This method allows accessing
68+
/// the runtime to interact with its features directly.
69+
///
70+
/// In a typical use case, you might need to share the same runtime between different
71+
/// modules of your project. For example, a module might require a `tokio::runtime::Handle`
72+
/// to spawn tasks on the same runtime, or the runtime itself to configure more complex
73+
/// behaviours.
74+
///
75+
/// # Example
76+
///
77+
/// ```
78+
/// use actix_rt::Runtime;
79+
///
80+
/// mod module_a {
81+
/// pub fn do_something(handle: tokio::runtime::Handle) {
82+
/// handle.spawn(async {
83+
/// // Some asynchronous task here
84+
/// });
85+
/// }
86+
/// }
87+
///
88+
/// mod module_b {
89+
/// pub fn do_something_else(rt: &tokio::runtime::Runtime) {
90+
/// rt.spawn(async {
91+
/// // Another asynchronous task here
92+
/// });
93+
/// }
94+
/// }
95+
///
96+
/// let actix_runtime = actix_rt::Runtime::new().unwrap();
97+
/// let tokio_runtime = actix_runtime.tokio_runtime();
98+
///
99+
/// let handle = tokio_runtime.handle().clone();
100+
///
101+
/// module_a::do_something(handle);
102+
/// module_b::do_something_else(tokio_runtime);
103+
/// ```
104+
///
105+
/// # Returns
106+
///
107+
/// An immutable reference to the `tokio::runtime::Runtime` instance associated with this
108+
/// `Runtime` instance.
109+
///
110+
/// # Note
111+
///
112+
/// While this method provides an immutable reference to the Tokio runtime, which is safe to share across threads,
113+
/// be aware that spawning blocking tasks on the Tokio runtime could potentially impact the execution
114+
/// of the Actix runtime. This is because Tokio is responsible for driving the Actix system,
115+
/// and blocking tasks could delay or deadlock other tasks in run loop.
116+
pub fn tokio_runtime(&self) -> &tokio::runtime::Runtime {
117+
&self.rt
118+
}
119+
64120
/// Runs the provided future, blocking the current thread until the future completes.
65121
///
66122
/// This function can be used to synchronously block the current thread until the provided

actix-rt/src/system.rs

+35
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,41 @@ impl SystemRunner {
203203
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
204204
}
205205

206+
/// Retrieves a reference to the underlying Actix runtime associated with this SystemRunner instance.
207+
///
208+
/// The Actix runtime is responsible for managing the event loop for an Actix system and executing asynchronous tasks.
209+
/// This method provides access to the runtime, allowing direct interaction with its features.
210+
///
211+
/// In a typical use case, you might need to share the same runtime between different
212+
/// parts of your project. For example, some components might require a [`actix_rt::Runtime`] to spawn tasks on
213+
/// the same runtime.
214+
///
215+
/// # Example
216+
///
217+
/// ```
218+
/// let system_runner = actix_rt::System::new();
219+
/// let actix_runtime = system_runner.runtime();
220+
///
221+
/// // Use the runtime to spawn an async task or perform other operations
222+
/// ```
223+
///
224+
/// Read more in the documentation for [`actix_rt::Runtime`]
225+
///
226+
/// # Returns
227+
///
228+
/// An immutable reference to the [`actix_rt::Runtime`] instance associated with this
229+
/// [`actix_rt::SystemRunner`] instance.
230+
///
231+
/// # Note
232+
///
233+
/// While this method provides an immutable reference to the Actix runtime, which is safe to share across threads,
234+
/// be aware that spawning blocking tasks on the Actix runtime could potentially impact system performance.
235+
/// This is because the Actix runtime is responsible for driving the system,
236+
/// and blocking tasks could delay other tasks in the run loop.
237+
pub fn runtime(&self) -> &crate::runtime::Runtime {
238+
&self.rt
239+
}
240+
206241
/// Runs the provided future, blocking the current thread until the future completes.
207242
#[track_caller]
208243
#[inline]

0 commit comments

Comments
 (0)