How does Python's Twisted Reactor work?

后端 未结 2 2021
温柔的废话
温柔的废话 2021-01-01 11:25

Recently, I\'ve been diving into the Twisted docs. From what I gathered, the basis of Twisted\'s functionality is the result of it\'s event loop called the \"Reactor\". The

2条回答
  •  佛祖请我去吃肉
    2021-01-01 12:05

    I will try to elaborate:

    • The program yields control and go to sleep on wait for events. I suppose the most interesting part here is event. Event is: on external demand (receiving network packet, click on a keyboard, timer, different program call) the program receives control (in some other thread or in special routine). Somehow the sleep in wait_for_events becomes interrupted and wait_for_events returns.

    • On that occurrence of control the event handler stores information of that event into some data structure, events, which later is used for doing something about that events (event->process). There can happen not only one, but many events in the time between entering and exiting of wait_for_events, all of them must be processed. The event->process() procedure is custom and should usually call the interesting part - user's twisted code.

提交回复
热议问题