Smithay / calloop

A callback-based Event Loop

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Non blocking event loop with calloop

chiru091096 opened this issue · comments

Hi,
I want to add functions to event loop from different parts of the code and want the execution of code to continue without any blocking. Is there a way to use calloop (dispatch method blocks till the execution of callback) so that it's not blocking the further execution of code. It'll be very helpful if some example code is provided with the functionality.

Invoking EventLoop::dispatch() with a timeout of 0 will cause it to process any available events and then return immediately. Is that what you need?

Not really.. I want to call event_loop.handle().insert_source() with a timer of 10 seconds. And then when I call event_loop.dispatch(None, &mut ()).expect("Error during event loop!"); I want the program to execute for 10 seconds without blocking. And then I want the call back to be fired..

Ah, so you want a background executor. Calloop does not spawn any background thread by design, in order to be compatible with non-threadsafe uses.

Mostly you have two options:

  • use another event-loop implementation that does spawn background threads
  • spawn the background thread yourself, and run your calloop event loop on it

Okay... thanks for the suggestions.
Do you have any suggestions for another event-loop which does background execution?
Also, It would be helpful if I can find any working sample for the second suggestion (running calloop in a background thread).

You can use tokio or smol as an executor in this case. They are both thread safe and generally work well with being run on a background thread.