Skip to content

Clarifications on how the Event Loop works and how to avoid blocking the event loop. #163

@vikigenius

Description

@vikigenius

I am new to concurrent programming. I read the Calloop book and I am struggling to understand how this enables concurrency.

From the documentation of the run method it seems like it will wait for timeout seconds for a new event. The moment we get a new event it dispatches the associated callback. I have a few questions

  1. Will I get no new events when the callback is being executed?
  2. What happens if we wait for timeout seconds and there is no new event.
  3. Maybe some extreme examples would help. What's the difference between setting timeout to zero vs setting it to None?

Here is a potential scenario:
If I have potentially blocking code in a callback such as reading a huge file, what should I do? Let's say I have an event that indicates that a large file is ready to be read. We will dispatch an associated callback for it. In the callback I want to read the file. But that's a blocking operation now. How would I do this? Will I need to spawn a thread to do this operation so that I can immediately return from the callback? If so it seems like any parallelism/concurrency comes from other code and not calloop itself?

I know this library was written with Smithay in mind. But a full concrete example would be helpful. The ZeroMQ example in the book is good for understanding event sources, but I am still struggling to understand the concurrency aspect. Maybe add to that example with a full main function that also handles some unix signals etc. and some actual processing of the messages: request/response etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions