@@ -61,6 +61,62 @@ impl Runtime {
61
61
self . local . spawn_local ( future)
62
62
}
63
63
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
+
64
120
/// Runs the provided future, blocking the current thread until the future completes.
65
121
///
66
122
/// This function can be used to synchronously block the current thread until the provided
0 commit comments