Git Product home page Git Product logo

Comments (3)

DoumanAsh avatar DoumanAsh commented on June 9, 2024

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.

gnattu avatar gnattu commented on June 9, 2024

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.

DoumanAsh avatar DoumanAsh commented on June 9, 2024

@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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.