Comments (3)
Because it is just callback, it is already technically speaking async.
You could simply send every event via channel in on_clipboard_change
and on_clipboard_error
and await for event from receiver end
from clipboard-master.
Unfortunately tokio does not like threads been non-async like this. From my testing, only the main thread is able to process the channel message sent from the monitor thread and if you want to sent the message across threads, other than main threads, the message never delivers unless the buffer is full and the channel is closed.
Let's look at this example:
async fn channel_proxy(mut rx: mpsc::Receiver<String>) {
while let Some(message) = rx.recv().await {
println!("Received: {}", message);
}
}
#[tokio::main]
async fn main() {
// Create a channel for communication
let (tx, mut rx) = mpsc::channel::<String>(32);
let (tx2, mut rx2) = mpsc::channel::<String>(32);
let _ = tokio::spawn(monitor_clipboard(tx));
let _ = tokio:: spawn(channel_proxy(rx));
loop{}
There will be nothing sent to the proxy, but the monitor is running well.
If we change the code to let the rx in the main thread, then everything works:
#[tokio::main]
async fn main() {
// Create a channel for communication
let (tx, mut rx) = mpsc::channel::<String>(32);
let (tx2, mut rx2) = mpsc::channel::<String>(32);
let _ = tokio::spawn(monitor_clipboard(tx));
while let Some(message) = rx.recv().await {
println!("Received: {}", message);
}
My educated guess would be that the non async loop in this lib prevents tokio's scheduler to schedule the receiver thread at all. More specifically, the std::thread::sleep
call in mac.rs
from clipboard-master.
@gnattu You should not spawn main loop of clipboard master on tokio runtime
Please create dedicated thread to run it.
Clipboard functionality is fully synchronous so it is impossible to have it integrated in tokio runtime as it is.
So your code should be something like that:
#[tokio::main]
async fn main() {
// Create a channel for communication
let (tx, mut rx) = mpsc::channel::<String>(32);
let (tx2, mut rx2) = mpsc::channel::<String>(32);
std::thread::spawn(move || {
let handler = <YourHandlerType>::new(tx);
clipboard_master::Master::new(handler).run();
})
let _ = tokio:: spawn(channel_proxy(rx));
loop{}
}
from clipboard-master.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clipboard-master.