how does a non-blocking event loop work?

∥☆過路亽.° 提交于 2020-01-04 04:08:12

问题


Twisted has a "non-blocking" event loop.

I understand what a blocking event loop does (sort of, from the Wikipedia page) but can't figure out how a non-blocking one does.


回答1:


while (true)
    wait_for_events
    handle_events

Basically, non-blocking event loop utilizes device that allows for waiting for multiple events simultaneously (select/poll on UNIX, WaitForMultipleEvents on Windows, epoll on Linux kqueue on FreeBSD etc). In each iteration of main loop, events (file descriptors, timers etc) are registered in some kind of handle. Then, a function that waits for events (eg. select) is invoked. This typically returns all events that happened during invocation of that function. Finally, loop handles that events - typically by invoking callbacks associated with events.

For details, see implementation of libevent or some GUI toolkit event loops - GTK+ or Qt.



来源:https://stackoverflow.com/questions/3071237/how-does-a-non-blocking-event-loop-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!