Method, device, equipment, storage medium and program product for processing events
By configuring coroutines for events and staggering their scheduling, the problem of insufficient event response and data processing capabilities in responsive programs is solved, and efficient event data processing and response is achieved.
Patent Information
- Application Number
- CN202411412013.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-10
- Publication Date
- 2025-09-16
- Estimated Expiration
- 2044-10-10
AI Technical Summary
Existing responsive programs lack event response and event data processing capabilities, making it difficult to efficiently process event data from multiple event sources.
By configuring a coroutine for an event and scheduling the coroutine to process the event data when it is detected that the event data is in a readable state, the coroutine is activated using a multi-functional thread, a communication channel is established, and multiple coroutines are interleaved to process the event data, avoiding blocking the detection of new events.
It improves the responsiveness of events and the efficiency of event data processing in responsive programs, simplifies thread pool configuration, and improves coroutine management and scheduling efficiency.
Smart Images

Figure CN119376976B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, specifically to technical fields such as event response, data processing, and thread scheduling, and more particularly to methods, devices, electronic devices, computer-readable storage media, and computer program products for processing events. Background Art
[0002] A program reactor is a technical architecture that is typically used to process and manage events and responses within a program. Alternatively, a program reactor can be understood as an event-driven network I / O design pattern.
[0003] In a responsive program, event sources (such as network connections and file system changes) are registered with an event dispatcher (Reactor). The event dispatcher is then responsible for listening to all registered event sources and forwarding events and their associated event data to the corresponding event handlers for processing. Therefore, improving the responsiveness of responsive programs to events and their ability to process their event data is a pressing need and deserves attention. Summary of the Invention
[0004] The embodiments of the present disclosure provide a method, apparatus, electronic device, computer-readable storage medium, and computer program product for processing an event.
[0005] In a first aspect, an embodiment of the present disclosure proposes a method for processing events, including: in response to detecting that an event has occurred, configuring a corresponding coroutine for the event; in response to detecting that event data of the event is in a readable state, scheduling a coroutine to process the event data.
[0006] In some embodiments, in response to detecting that the event data of an event is in a readable state, scheduling a coroutine to process the event data includes: in response to detecting that the event data of an event is in a readable state, scheduling a coroutine to process the event data, and detecting whether a new event occurs, and / or the reading status of the event data of an event that has occurred in a non-blocking manner.
[0007] In some embodiments, in response to detecting that an event has occurred, configuring a corresponding coroutine for the event, including: in response to detecting that an event has occurred, communicating the event to a multi-function thread in a thread pool; using the multi-function thread to configure a corresponding coroutine for the event from a set of coroutines associated with the multi-function thread, and establishing a communication pipe for the multi-function thread to communicate with the event source of the event; using the multi-function thread to activate the coroutine, wherein the multi-function thread, in response to detecting that event data of the event from the communication pipe for communicating between the multi-function thread and the event source of the event is in a readable state, schedules the activated coroutine to process the event data.
[0008] In some embodiments, the method further includes: detecting whether an event message appears in the communication channel, wherein the event message indicates that a new event has occurred, or that the reading state of event data of an event that has occurred has changed to a readable state.
[0009] In some embodiments, it also includes: in response to the detection function being called by the coroutine, detecting whether a new event has occurred, and the reading status of the event data of the event that has occurred, wherein the coroutine calls the detection function in response to the processing progress of processing the event data reaching the detection point position.
[0010] In some embodiments, in response to detecting that event data of an event is in a readable state, scheduling a coroutine to process the event data includes: in response to detecting that event data corresponding to at least two coroutines are both in a readable state, adding a scheduling action for the coroutine to a scheduling queue; based on the scheduling queue, interleavingly scheduling at least two coroutines to process the corresponding event data.
[0011] In some embodiments, at least two coroutines are interleavedly scheduled to process corresponding event data, including: in response to the processing progress of the event data corresponding to the first coroutine currently scheduled in the scheduling queue reaching a yield point, and there is a second coroutine that can be scheduled in the scheduling queue, switching out the first coroutine and scheduling the second coroutine.
[0012] In some embodiments, it also includes: in response to the processing progress of the event data corresponding to the first coroutine currently scheduled in the scheduling queue reaching the yield point, and there is no second coroutine that can be scheduled in the scheduling queue, cutting out the first coroutine and detecting whether a new event occurs in a blocking manner, and / or the reading status of the event data of the event that has occurred.
[0013] In a second aspect, an embodiment of the present disclosure proposes a device for processing events, comprising: a coroutine configuration unit, configured to configure a corresponding coroutine for the event in response to detecting that an event has occurred; and a coroutine scheduling unit, configured to schedule a coroutine to process the event data in response to detecting that the event data of the event is in a readable state.
[0014] In some embodiments, the coroutine scheduling unit is further configured to: include: in response to detecting that the event data of an event is in a readable state, scheduling the coroutine to process the event data, and detecting whether a new event occurs, and / or the reading status of the event data of an event that has occurred in a non-blocking manner.
[0015] In some embodiments, the coroutine configuration unit includes: an event communication sub-unit, configured to communicate the event to the multi-function thread in the thread pool in response to detecting that an event has occurred; a first multi-function thread utilization sub-unit, configured to utilize the multi-function thread to configure a corresponding coroutine for the event from a group of coroutines associated with the multi-function thread, and establish a communication pipe for the multi-function thread to communicate with the event source of the event; a second multi-function thread utilization sub-unit, configured to utilize the multi-function thread to activate the coroutine, wherein the multi-function thread schedules the activated coroutine to process the event data in response to detecting that the event data of the event is in a readable state from the communication pipe for communicating between the multi-function thread and the event source of the event.
[0016] In some embodiments, it also includes: an event message detection unit configured to detect whether an event message appears in the communication pipeline, wherein the event message indicates that a new event has occurred, or the reading state of event data of an event that has occurred has changed to a readable state.
[0017] In some embodiments, it also includes: a response detection unit, which is configured to detect whether a new event has occurred and the reading status of the event data of the event that has occurred in response to the detection function being called by the coroutine, wherein the coroutine calls the detection function in response to the processing progress of the event data reaching the detection point position.
[0018] In some embodiments, a coroutine scheduling unit includes: a scheduling queue maintenance subunit, configured to add a scheduling action for the coroutine to a scheduling queue in response to detecting that event data corresponding to at least two coroutines are in a readable state; and a coroutine scheduling subunit, configured to interleavely schedule at least two coroutines to process corresponding event data based on the scheduling queue.
[0019] In some embodiments, at least two coroutines are interleavedly scheduled to process corresponding event data, including: in response to the processing progress of the event data corresponding to the first coroutine currently scheduled in the scheduling queue reaching a yield point, and there is a second coroutine that can be scheduled in the scheduling queue, switching out the first coroutine and scheduling the second coroutine.
[0020] In some embodiments, it also includes: a post-yield event detection unit, which is configured to cut out the first coroutine in response to the processing progress of the event data corresponding to the first coroutine currently scheduled in the scheduling queue reaching the yield point position, and there is no second coroutine that can be scheduled in the scheduling queue, and detect whether a new event occurs in a blocking manner, and / or the reading status of the event data of the event that has occurred.
[0021] In a third aspect, an embodiment of the present disclosure provides an electronic device comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can implement the method for processing events as described in any implementation method in the first aspect when executing the instructions.
[0022] In a fourth aspect, an embodiment of the present disclosure provides a non-transitory computer-readable storage medium storing computer instructions, which are used to enable a computer to implement the method for processing events as described in any implementation manner in the first aspect when executed.
[0023] In a fifth aspect, an embodiment of the present disclosure provides a computer program product comprising a computer program, which, when executed by a processor, can implement the method for processing events as described in any implementation manner in the first aspect.
[0024] The event processing method, apparatus, electronic device, computer-readable storage medium, and computer program product provided by the embodiments of the present disclosure are applied to a thread of a responsive program and can respond to a detected event by configuring a corresponding coroutine for the event. Then, if the event data of the detected event is readable by the thread, the coroutine is scheduled to process the event data.
[0025] The present disclosure can not only improve the ability to respond to events, but also improve the scheduling and processing efficiency of event data processing.
[0026] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present disclosure, nor are they intended to limit the scope of the present disclosure. Other features of the present disclosure will become readily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] Other features, objects and advantages of the present disclosure will become more apparent from a reading of the detailed description of non-limiting embodiments made with reference to the following drawings:
[0028] Figure 1 is an exemplary system architecture in which the present disclosure may be applied;
[0029] Figure 2 A flowchart of a process for handling an event provided by an embodiment of the present disclosure;
[0030] Figure 3 A flowchart of another process for handling events provided by an embodiment of the present disclosure;
[0031] Figure 4A flowchart of a process for handling an event in an application scenario provided by an embodiment of the present disclosure;
[0032] Figure 5 A structural block diagram of a device for processing events provided in an embodiment of the present disclosure;
[0033] Figure 6 A schematic structural diagram of an electronic device suitable for executing a method for processing events provided by an embodiment of the present disclosure. DETAILED DESCRIPTION
[0034] The following description of exemplary embodiments of the present disclosure is made in conjunction with the accompanying drawings, including various details of the embodiments of the present disclosure to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those skilled in the art that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description. It should be noted that the embodiments in the present disclosure and the features in the embodiments can be combined with each other unless there is a conflict.
[0035] In addition, in the technical solutions involved in this disclosure, the acquisition, storage, use, processing, transportation, provision and disclosure of user personal information involved (for example, the events involved in the subsequent disclosure may correspond to user personal information, or may actually be events that require the use of user personal information) shall comply with the provisions of relevant laws and regulations and shall not violate public order and good morals.
[0036] Figure 1 An exemplary system architecture 100 is shown to which embodiments of the method, apparatus, electronic device, and computer-readable storage medium for processing events disclosed herein can be applied.
[0037] like Figure 1 As shown, system architecture 100 may include terminal devices 101, 102, 103, a network 104, and a server 105. Network 104 is a medium for providing communication links between terminal devices 101, 102, 103 and server 105. Network 104 may include various connection types, such as wired or wireless communication links or fiber optic cables.
[0038] Users can use terminal devices 101, 102, 103 to interact with server 105 via network 104 to receive or send messages, etc. Terminal devices 101, 102, 103 and server 105 may be installed with various applications for implementing information communication between the two.
[0039] Terminal devices 101, 102, 103 and server 105 can be either hardware or software. When terminal devices 101, 102, 103 are hardware, they can be various electronic devices with display screens, including but not limited to smartphones, tablet computers, laptop computers, and desktop computers. When terminal devices 101, 102, 103 are software, they can be installed in the electronic devices listed above. They can be implemented as multiple software or software modules, or as a single software or software module, and are not specifically limited here. When server 105 is hardware, it can be implemented as a distributed server cluster consisting of multiple servers, or as a single server. When the server is software, it can be implemented as multiple software or software modules, or as a single software or software module, and are not specifically limited here.
[0040] For example, an event processing application of the nature and type of event processing may be configured in the server 105. In this case, the terminal devices 101, 102, and 103 can act as event sources and communicate with the server 105 based on their locally configured event processing application to utilize the server 105 for "event processing."
[0041] Accordingly, the server 105 can respond based on the responsive programming approach. Specifically, a thread (e.g., thread 110) in the server 105 that is used for the responsive programming can, in response to detecting the occurrence of an event (e.g., an "event" in which the terminal devices 101, 102, and 103 request the server 105 to process a task or perform an action), configure a corresponding coroutine for the event; then, in response to the event data being in a readable state, thread 110 schedules a coroutine to process the event data.
[0042] It should be noted that, in addition to being acquired from terminal devices 101, 102, and 103 via network 104, "events" can also be local "events" on server 105. For example, pending tasks and pending events pre-stored locally on server 105 through various means. Therefore, when server 105 detects that these events are already locally stored and need to be executed (e.g., events already locally stored before processing begins), it can choose to directly acquire these data locally. In this case, exemplary system architecture 100 may also exclude terminal devices 101, 102, 103 and network 104.
[0043] Due to the demand for computing resources and computing power, the methods for processing events provided in the subsequent embodiments of the present disclosure are generally executed by the server 105 with stronger computing power and more computing resources. Accordingly, the device for processing events is generally also set in the server 105 (for example, set and applied to the thread 110 of the responsive program). However, it should also be pointed out that when the terminal devices 101, 102, and 103 also have computing power and computing resources that meet the requirements, the terminal devices 101, 102, and 103 can also complete the above-mentioned various operations that are originally assigned to the server 105 through the event processing applications installed thereon, and then output the same results as the server 105 (for example, the thread 110 of the responsive program is also in the terminal devices 101, 102, and 103). In particular, when there are multiple terminal devices with different computing capabilities, if the event processing application determines that the terminal device it is in has stronger computing capabilities and more remaining computing resources, it can allow the terminal device to perform the above-mentioned operations, thereby appropriately reducing the computing pressure on the server 105. Accordingly, the device of the event processing application can also be set in the terminal devices 101, 102, and 103. In this case, the exemplary system architecture 100 may also not include the server 105 and the network 104.
[0044] It should be understood that Figure 1 The number of terminal devices, networks and servers in the embodiment is merely illustrative. Any number of terminal devices, networks and servers may be provided as required.
[0045] Please refer to Figure 2 , Figure 2 A flowchart of a process for processing an event provided by an embodiment of the present disclosure includes process 200. Process 200 includes the following steps:
[0046] Step 201, in response to detecting that an event has occurred, configuring a corresponding coroutine for the event;
[0047] In the embodiments of the present disclosure, for example, Figure 1 The thread 110 shown is applied to the responsive program as the execution subject.
[0048] In some embodiments, the execution entity can communicate with objects and devices such as terminal devices 101, 102, and 103 based on a communication channel. Accordingly, the execution entity can choose to determine whether an event message has occurred based on probing the communication channel with these objects and devices. For example, the communication channel can be a socket in a responsive programming model.
[0049] Socket is a foundation of network programming, which can provide a mechanism for data exchange between the execution subject and other components in the server 105, or between the terminal devices 101, 102 and 103. Accordingly, through Socket, the execution subject can realize communication with other objects and devices.
[0050] Accordingly, the execution entity can detect these communication channels to determine whether any event occurs.
[0051] Specifically, the execution subject may choose to detect whether an event message appears in the communication channel, where the event message indicates that a new event has occurred or that the read status of the event data of an event that has occurred has changed to a readable state. Accordingly, if a new event occurs or the read status of the event data of an event that has occurred has changed (from unreadable to readable), the execution subject may respond to it.
[0052] First, the case where a new event occurs is described (for example, such an event can actually be exemplified as a socket connection being established). If the execution subject determines that a new event has occurred, it can respond to it.
[0053] For example, as discussed below, the execution entity can configure a corresponding coroutine for the event indicated by the event message. For example, when a socket connection is established, the execution entity can determine that an event has occurred in the communication channel and configure a corresponding coroutine for the event.
[0054] As a result, the execution subject can determine whether an event has occurred based on the detection of the communication channel without having to wait for or request an indication of the event occurrence from the event source. This approach can improve the execution subject's response efficiency to events.
[0055] Next, if the execution body detects that an event has occurred, the execution body can choose to respond to it and configure the corresponding coroutine for the event.
[0056] A coroutine is a lightweight thread that can be suspended while waiting for certain operations (such as I / O), allowing other coroutines to run, thereby improving concurrency. Alternatively, a coroutine can be a special function that can be suspended during execution and resumed later.
[0057] For example, a coroutine can execute processing actions based on assigned tasks and events while being called by an execution subject. If a specific event occurs during this process, the coroutine can be temporarily suspended so that it can be temporarily released (processed). This allows the execution subject to use the processing resources and processing threads corresponding to the coroutine to handle other tasks.
[0058] Correspondingly, after other "specific events" are processed, the execution subject can choose to continue scheduling the coroutine to resume execution and continue to complete the events that were not completed previously.
[0059] Step 202 : In response to detecting that the event data of the event is in a readable state, scheduling a coroutine to process the event data.
[0060] In an embodiment of the present disclosure, based on step 201, as discussed above, this step is intended to detect the reading status of event data corresponding to the (occurred) event by the above-mentioned execution entity (for example, the event may be an event message sent by a terminal device arriving at the server where the execution entity is located).
[0061] During the detection process, if the event data reading status of the event that has occurred changes from an unreadable state to a readable state (for example, because the event data of the event has been prepared, it can be provided to the execution subject or other threads by the event source, resulting in the reading status changing from an unreadable state to a readable state), the execution subject can choose to call the corresponding coroutine to actually process the event data.
[0062] In some optional implementations of this embodiment, the action corresponding to the event may actually include a data pulling action from the event source. That is, the event data is actually provided to the execution subject through the "pulling action" executed by the coroutine.
[0063] In such a case, the execution subject may choose to detect whether the event data in the event source is already in a state that can be pulled, to determine whether the reading state of the event data of the event is already in a readable state.
[0064] Then, the execution body can schedule the coroutine accordingly, and the coroutine will actually pull and process the event data.
[0065] For example, the event source can be configured to expose and provide a data interface for pulling event data to the execution entity after preparing the event data locally. Accordingly, the execution entity can determine that the event data is ready for reading when it detects that the event source has exposed and provided a data interface that can be used to pull event data.
[0066] In some optional implementations of this embodiment, when the execution subject schedules the coroutine to process event data, it can also continue to detect whether a new event has occurred and / or the reading status of the event data of the event that has occurred in a non-blocking manner. That is, when the execution subject detects that the event data of an event is in a readable state and schedules the coroutine to process the corresponding event data, it can choose to detect whether a new event has occurred and / or the (data) reading status of the event data of the event that has occurred in a non-blocking manner. The non-blocking method can, for example, be a method of communicating between the execution subject and other event sources in an asynchronous state using a message mechanism of a Socket event.
[0067] In this way, the execution subject will not interrupt or block the detection of new events and new event data during the process of scheduling the coroutine to process event data.
[0068] This can prevent the detection of new events and new event data from being blocked by the scheduling coroutine, affecting the execution efficiency of the execution subject for the event.
[0069] The event processing method provided by the disclosed embodiments is applied to a thread of a responsive program. In response to detecting an event, a corresponding coroutine is configured for the event. In response to detecting that the event data is in a readable state, the coroutine is scheduled to process the event data. This not only improves the responsiveness to events, but also improves the scheduling and processing efficiency of event data.
[0070] In some embodiments, the execution subject may only provide detection, and configure a coroutine by utilizing a multi-functional thread in a thread pool, and call the coroutine to specifically process the event data of the event.
[0071] Specifically, in response to detecting an event, the execution subject can communicate the event to a multi-function thread in the thread pool. The multi-function thread in the thread pool can "assign" and "bind" the event to a coroutine in a group of coroutines corresponding to the multi-function thread and establish a communication channel with the event source. The execution subject can then use the multi-function thread to activate the coroutine to make the coroutine available.
[0072] Subsequently, as an alternative or in addition, the multi-function thread can detect the read status of the "event data." That is, the multi-function thread detects the communication channel between it and the event source to detect the read status of the event data. If the multi-function thread detects that the read status of the event data has changed to a readable state, it can schedule the currently activated coroutine to process the event data.
[0073] This approach allows the execution subject to only continuously and persistently detect whether new events have occurred, allowing the execution subject to focus more on detecting whether an event has occurred, thereby improving the efficiency of responding to those events. Furthermore, this approach simplifies the thread pool configuration by configuring multi-function threads. This allows a single "multi-function thread" to fully implement actions such as configuring coroutines, reading status detection, and scheduling coroutines, simplifying the thread pool architecture and improving thread utilization within the thread pool.
[0074] In some embodiments, to further simplify and reduce configuration difficulty, the execution entity can also choose to schedule the coroutine itself, that is, no longer relying on the multi-function thread in the thread pool to ultimately schedule the coroutine. This not only improves the management, scheduling, and control efficiency of the coroutine (for example, it eliminates the need for "communication" with the thread pool), but also eliminates the need for additional thread pool configuration, simplifying configuration and saving configuration resources.
[0075] For this, please refer to Figure 3 , Figure 3 A flowchart of another process for processing an event provided by an embodiment of the present disclosure includes process 300. Process 300 includes the following steps:
[0076] Step 301: In response to detecting that an event has occurred, a corresponding coroutine is configured for the event.
[0077] Specifically, when the execution subject detects that an event has occurred, it can respond to it by directly generating a coroutine and assigning it to the event to configure the corresponding coroutine for the event.
[0078] Step 302: Detect the number of coroutines whose corresponding event data is in a readable state.
[0079] Specifically, the execution subject can detect whether the event data corresponding to the coroutine is already in a readable state in the manner discussed above.
[0080] Furthermore, considering that this approach cannot utilize the thread pool's scheduling capabilities, it may cause "conflicts" due to at least two event data being in a readable state at the same time. To avoid such conflicts, the execution entity can also simultaneously detect the number of coroutines whose corresponding event data is in a readable state.
[0081] If only the event data corresponding to one coroutine is in a readable state, the execution entity may execute step 303 to schedule the coroutine.
[0082] If the event data corresponding to at least two coroutines are in a readable state, the execution entity may execute step 304 .
[0083] Step 304: Add the scheduling action for the coroutine to the scheduling queue.
[0084] Specifically, if there are at least two coroutines whose corresponding event data are both in a readable state, the execution entity can manage the calling order of these coroutines based on the scheduling queue. The execution entity can add the scheduling action for the coroutine to the scheduling queue.
[0085] For example, the execution subject may sequentially store and add each coroutine to the scheduling queue based on the time sequence in which the corresponding event data changes to a readable state.
[0086] In some embodiments, if the event data corresponding to two coroutines changes to a readable state at the same time, the execution entity can sequentially store and add the corresponding scheduling actions of each coroutine to the scheduling queue based on the generation and binding time of each coroutine. Accordingly, in some embodiments, the subsequent scheduling queue can be used to indicate that each coroutine should be called sequentially based on this storage order.
[0087] It should be understood that in some embodiments, it may not be necessary to schedule coroutines based on the "scheduling logic" of "the order in which event data becomes readable, and the order in which coroutines are generated." In this case, the execution entity may also choose to select the next scheduled task from the scheduling sequence based on, for example, the event processing priority, or randomly.
[0088] Accordingly, in response to these "scheduling logics," the execution entity can place a coroutine in the scheduling queue only when the corresponding event data's read status changes to a readable state. For example, without the need for "scheduling logic that prioritizes coroutine generation based on the order in which event data changes to a readable state," the execution entity can arbitrarily select a coroutine that meets the requirements for invocation and place other coroutines that meet the requirements in the scheduling queue.
[0089] Step 305: Based on the scheduling queue, at least two coroutines are called alternately to process corresponding event data.
[0090] Specifically, the execution subject can interleave the calls to the various coroutines based on the order indicated by the scheduling queue in combination with actual needs to process the corresponding events. For example, corresponding to the above discussion, in accordance with the corresponding storage order in the scheduling queue. Or, arbitrarily select a coroutine from the scheduling queue, etc., which will not be repeated here. In some optional implementations of this embodiment, for the interleaved scheduling of at least two coroutines, the execution subject can determine whether there is a second coroutine in the scheduling queue that can be (continued) scheduled after the processing progress of the event data corresponding to the currently scheduled first coroutine in the scheduling queue reaches the yield point.
[0091] Typically, the yield point position can be, for example, the progress position corresponding to the sleep-type operation, the progress position for the completion of the processing of event data, etc. Correspondingly, when the processing progress of the event data corresponding to the currently scheduled first coroutine reaches these (progress) positions (for example, the position of executing to the sleep-type operation), it can choose to yield. At the same time, if the first coroutine is to be yielded (or cut out), and there is a second coroutine that can be scheduled in the scheduling queue, the second coroutine can be scheduled after the first coroutine is yielded, so as to realize the staggered scheduling of the coroutines. In this way, the coroutine can directly switch to other coroutines after cutting out, and will not return to the thread local. In this way, the scheduling efficiency of the coroutine can be improved.
[0092] In some optional implementations of this embodiment, if the processing progress of the event data corresponding to the first coroutine currently scheduled in the scheduling queue reaches the yield point (for example, the position of executing to the sleep type operation), and there is no second coroutine that can be scheduled in the scheduling queue, the first coroutine can be chosen to yield, and the execution subject can detect whether a new event has occurred and / or the reading status of the event data of the event that has occurred in a blocking manner (that is, whether it has changed to a readable state). Thus, when there is no subsequent coroutine that can be scheduled, the execution subject, that is, the thread, is used to continue the detection.
[0093] Typically, the scheduling order can be determined by the scheduling queue's priority algorithm and the actual situation during the execution of the event. For example, during the processing of event data for event 1, if the processing progress reaches a sleep-like operation, the coroutine processing event 1 can be switched out and the coroutine processing event 2 can be switched in. Correspondingly, if the coroutine processing event 2 also reaches a sleep-like operation, and the waiting time for the coroutine processing event 1 has expired, the execution body can choose to switch back to the coroutine processing event 1.
[0094] In addition, if the waiting time of the coroutine processing event 1 has expired, the execution body can similarly schedule other coroutines to process, such as event 3 and event 4, based on whether there are other coroutines in the scheduling queue that can be scheduled to process the event.
[0095] In some scenarios, if the wait time of the coroutine handling event 1 has not expired and there are no other events, or in other words, the coroutine can be scheduled, the execution body will wait until the wait time of either the coroutine handling event 1 or the coroutine handling event 2 expires. In this way, scheduling is achieved in an interleaved manner.
[0096] Blocking modes can include standard communication mode, buffered communication mode, and so on. Similarly, using Socket as an example, in blocking mode, when sending a message using a protocol like TCP, if the lower-layer protocol does not have space to store the user data, the application process will block and wait until the protocol has free space. Unlike non-blocking mode, where the call returns immediately without waiting, in blocking mode, if the application process calls a receive function to receive a message but no data has arrived, the call will block until data arrives or an error occurs.
[0097] This can avoid incorrectly occupying the coroutine for a long time, which may cause the coroutine to be unable to complete the event data processing action and be unable to give up.
[0098] In some embodiments, for a coroutine, it can usually also require the execution subject to detect the event data of new events and events that have occurred in its corresponding communication pipeline (for example, whether new event data has occurred, the reading status of the new event data). For example, one or more detection point positions can be configured in the processing progress of the coroutine processing event data. Accordingly, after the processing progress of the coroutine processing event data reaches these detection point positions, it can choose to require the execution subject to perform "detection". In this way, the coroutine can continuously determine the action mode based on the detection results of the thread (for example, when the currently processed event data is updated, the coroutine can continue to process based on the updated event data).
[0099] For example, a coroutine can request or instruct the thread that is the executing subject to probe its corresponding communication channel by calling a probe function. Typically, when performing such a "probe", the executing subject can only detect whether the event data (e.g., data content) of the event corresponding to the coroutine has changed, or whether there is a new event configured for the coroutine, through the probe of the corresponding communication channel.
[0100] In some optional implementations of this embodiment, for the behavior of the coroutine calling the detection function, the execution subject can also choose to provide a comprehensive "detection" for more and all coroutines at the same time, that is, the actual response method of the execution subject to the detection function can be adjusted to detect all communication channels, rather than just the communication channel of the coroutine that calls the detection function. That is, the execution subject can choose to "detect" the reading status of the events and event data of all coroutines bundled to the execution subject in full, and even detect the communication channels corresponding to other threads. In this way, the reading status of events and event data can be fully maintained in a more proactive, timely and positive manner to improve the detection quality.
[0101] To deepen understanding, this disclosure also provides a specific implementation solution in combination with a specific application scenario, see Figure 4 Process 400 is shown.
[0102] When detecting the occurrence of event messages 411 and 412 , thread 410 of the responsive program may determine that events 421 and 422 have occurred accordingly.
[0103] For events 421 and 422 , illustratively, thread 410 may execute S401 to generate a coroutine 431 corresponding to event 421 and a coroutine 432 corresponding to event 422 .
[0104] Next, thread 410 may (continuously) execute S402 to detect the read status of event data 423 of event 421 and event data 424 of event 422 .
[0105] Subsequently, if the event data 423 and 424 are “ready”, the thread 410 may detect the data call interface 425 of the event data 423 and the data call interface 426 of the event data 423 .
[0106] In this case, in response to the event data 423 and 424 being in a readable state, thread 410 may execute S403 , schedule coroutine 431 to process event data 423 , and store the scheduling action of coroutine 432 in scheduling queue 441 .
[0107] As discussed above, for ease of understanding, a scenario where priority is given to scheduling a coroutine generated earlier is used as an example, with coroutine 431 generated earlier than coroutine 432. Accordingly, thread 410 prioritizes calling coroutine 431, which was generated earlier, and stores (the scheduling action of) coroutine 432 in dispatch queue 441.
[0108] Next, if the processing progress of the coroutine 431 reaches a yield point and there is a scheduling action for the coroutine 432 in the scheduling queue 441, the thread 410 can execute S404 to continue scheduling 432.
[0109] Further references Figure 5 As an implementation of the methods shown in the above figures, the present disclosure provides an embodiment of a device for processing events. Figure 2 Corresponding to the method embodiment shown, the device can be specifically applied to various electronic devices.
[0110] like Figure 5 As shown, the event processing apparatus 500 of this embodiment may include: a coroutine configuration unit 501 and a coroutine scheduling unit 502. The coroutine configuration unit 501 is configured to, in response to detecting that an event has occurred, configure a corresponding coroutine for the event; and the coroutine scheduling unit 502 is configured to, in response to detecting that event data of the event is in a readable state, schedule a coroutine to process the event data.
[0111] In this embodiment, in the device for processing events 500, the specific processing of the coroutine configuration unit 501 and the coroutine scheduling unit 502 and the technical effects thereof can be referred to respectively. Figure 2 The relevant descriptions of steps 201-202 in the corresponding embodiment are not repeated here.
[0112] In some embodiments, the coroutine scheduling unit 502 is further configured to: include: in response to detecting that the event data of an event is in a readable state, scheduling a coroutine to process the event data, and detecting whether a new event occurs, and / or the reading status of the event data of an event that has occurred in a non-blocking manner.
[0113] In some optional implementations of this embodiment, the coroutine configuration unit 501 includes: an event communication sub-unit, configured to communicate the event to the multi-function thread in the thread pool in response to detecting the occurrence of an event; a first multi-function thread utilization sub-unit, configured to utilize the multi-function thread to configure a corresponding coroutine for the event from a group of coroutines associated with the multi-function thread, and establish a communication pipeline for the multi-function thread to communicate with the event source of the event; a second multi-function thread utilization sub-unit, configured to utilize the multi-function thread to activate the coroutine, wherein the multi-function thread, in response to detecting that the event data of the event is in a readable state from the communication pipeline for communicating between the multi-function thread and the event source of the event, schedules the activated coroutine to process the event data.
[0114] In some optional implementations of this embodiment, the device 500 also includes: an event message detection unit, configured to detect whether an event message appears in the communication pipeline, wherein the event message indicates that a new event has occurred, or that the reading status of event data of an event that has occurred has changed to a readable state.
[0115] In some optional implementations of this embodiment, the device 500 also includes: a response detection unit, configured to detect whether a new event has occurred and the reading status of event data of an event that has occurred in response to the detection function being called by the coroutine, wherein the coroutine calls the detection function in response to the processing progress of processing the event data reaching the detection point position.
[0116] In some embodiments, the coroutine scheduling unit 502 includes: a scheduling queue maintenance sub-unit, configured to add a scheduling action for the coroutine to the scheduling queue in response to detecting that the event data corresponding to at least two coroutines are in a readable state; and a coroutine scheduling sub-unit, configured to schedule at least two coroutines to process the corresponding event data in an interleaved manner based on the scheduling queue.
[0117] In some embodiments, at least two coroutines are interleavedly scheduled to process corresponding event data, including: in response to the processing progress of the event data corresponding to the first coroutine currently scheduled in the scheduling queue reaching a yield point, and there is a second coroutine that can be scheduled in the scheduling queue, switching out the first coroutine and scheduling the second coroutine.
[0118] In some embodiments, the device 500 also includes: a post-yield event detection unit, which is configured to cut out the first coroutine in response to the processing progress of the event data corresponding to the first coroutine currently scheduled in the scheduling queue reaching the yield point position, and there is no second coroutine that can be scheduled in the scheduling queue, and detect whether a new event occurs in a blocking manner, and / or the reading status of the event data of the event that has occurred.
[0119] This embodiment exists as an apparatus embodiment corresponding to the above-mentioned method embodiment. The apparatus for processing events provided by this embodiment can not only improve the ability to respond to events, but also improve the scheduling and processing efficiency of event data of events.
[0120] According to an embodiment of the present disclosure, the present disclosure also provides an electronic device, a readable storage medium, and a computer program product.
[0121] Figure 6A schematic block diagram of an example electronic device 600 that can be used to implement embodiments of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital assistants, cellular phones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are provided as examples only and are not intended to limit the implementation of the present disclosure described and / or claimed herein.
[0122] like Figure 6 As shown, the device 600 includes a computing unit 601, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 602 or a computer program loaded from a storage unit 608 into a random access memory (RAM) 603. Various programs and data required for the operation of the device 600 can also be stored in the RAM 603. The computing unit 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0123] Various components in device 600 are connected to I / O interface 605, including an input unit 606, such as a keyboard, mouse, etc.; an output unit 607, such as various types of displays, speakers, etc.; a storage unit 608, such as a magnetic disk, optical disk, etc.; and a communication unit 609, such as a network card, modem, wireless communication transceiver, etc. The communication unit 609 allows device 600 to exchange information / data with other devices via a computer network such as the Internet and / or various telecommunication networks.
[0124] The computing unit 601 can be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 601 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units that run machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 601 performs the various methods and processes described above, such as the method for processing events. For example, in some embodiments, the method for processing events can be implemented as a computer software program that is tangibly contained in a machine-readable medium, such as a storage unit 608. In some embodiments, part or all of the computer program can be loaded and / or installed on the device 600 via the ROM 602 and / or the communication unit 609. When the computer program is loaded into the RAM 603 and executed by the computing unit 601, one or more steps of the method for processing events described above can be performed. Alternatively, in other embodiments, the computing unit 601 can be configured to perform the method for processing events by any other appropriate means (e.g., by means of firmware).
[0125] Various embodiments of the systems and techniques described herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), system-on-chip systems (SOCs), programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include being implemented in one or more computer programs that are executable and / or interpreted on a programmable system that includes at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0126] The program code for implementing the method of the present disclosure can be written in any combination of one or more programming languages. These program codes can be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device so that when the program code is executed by the processor or controller, the functions / operations specified in the flow chart and / or block diagram are implemented. The program code can be executed entirely on the machine, partially on the machine, as a stand-alone software package, partially on the machine and partially on a remote machine, or entirely on a remote machine or server.
[0127] In the context of the present disclosure, a machine-readable medium can be a tangible medium that can contain or store a program for use by or in conjunction with an instruction execution system, device or equipment. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium can include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0128] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0129] The systems and techniques described herein can be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer having a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network (LAN), a wide area network (WAN), and the Internet.
[0130] A computer system may include a client and a server. The client and server are typically remote from each other and typically interact via a communication network. This client-server relationship arises through computer programs running on the respective computers, creating a client-server relationship. The server may be a cloud server, also known as a cloud computing server or cloud host, a host product within a cloud computing service ecosystem that addresses the management difficulties and limited scalability of traditional physical hosts and virtual private server (VPS) services. Servers may also be classified as distributed system servers or servers integrated with blockchain.
[0131] According to the technical solution of the embodiment of the present disclosure, not only the response capability to events can be improved, but also the scheduling and processing efficiency of event data processing can be improved.
[0132] It should be understood that the various forms of processes shown above can be used to reorder, add, or delete steps. For example, the steps described in this disclosure can be performed in parallel, sequentially, or in a different order, as long as the desired results of the technical solutions provided by this disclosure can be achieved. This is not limited herein.
[0133] The above specific embodiments do not constitute a limitation on the scope of protection of this disclosure. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may be made based on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this disclosure shall be included within the scope of protection of this disclosure.
Claims
1. A method for handling events, applied to a thread of a responsive program, comprising: In response to detecting that an event has occurred, configuring a corresponding coroutine for the event; In response to detecting that the event data of the event is in a readable state, scheduling the coroutine to process the event data, including: in response to detecting that the event data of the event is in a readable state, scheduling the coroutine to process the event data, and detecting whether a new event occurs, and / or the reading status of the event data of an event that has occurred in a non-blocking manner.
2. The method according to claim 1, wherein In response to detecting that an event has occurred, configuring a corresponding coroutine for the event includes: In response to detecting that an event has occurred, communicating the event to a multi-function thread in the thread pool; Using the multifunctional thread, from a group of coroutines associated with the multifunctional thread, configure a corresponding coroutine for the event, and establish a communication channel for the multifunctional thread to communicate with an event source of the event; The coroutine is activated by the multi-function thread, wherein the multi-function thread schedules the activated coroutine to process the event data in response to detecting that event data of the event is in a readable state from a communication pipe through which the multi-function thread communicates with an event source of the event.
3. The method according to claim 1, further comprising: Detect whether an event message appears in the communication channel, wherein the event message indicates that a new event has occurred, or that the reading state of event data of an event that has occurred has changed to the readable state.
4. The method according to claim 1, further comprising: In response to the detection function being called by the coroutine, whether a new event occurs and the reading status of the event data of the event that has occurred are detected, wherein the coroutine calls the detection function in response to the processing progress of processing the event data reaching the detection point position.
5. The method according to claim 1, wherein In response to detecting that the event data of the event is in a readable state, scheduling the coroutine to process the event data includes: In response to detecting that event data corresponding to at least two of the coroutines are both in a readable state, adding a scheduling action for the coroutine to a scheduling queue; Based on the scheduling queue, at least two of the coroutines are scheduled to process corresponding event data in an interleaved manner.
6. The method according to claim 5, wherein: Interleavingly scheduling at least two of the coroutines to process corresponding event data, including: In response to a processing progress of event data corresponding to a first coroutine currently scheduled in a scheduling queue reaching a yield point, and a second coroutine that can be scheduled exists in the scheduling queue, the first coroutine is cut out and the second coroutine is scheduled.
7. The method according to claim 6, further comprising: In response to the processing progress of event data corresponding to the first coroutine currently scheduled in the scheduling queue reaching a yield point, and there is no second coroutine that can be scheduled in the scheduling queue, the first coroutine is cut out and the occurrence of a new event and / or the reading status of the event data of the event that has occurred is detected in a blocking manner.
8. A device for processing events, applied to a thread of a responsive program, comprising: a coroutine configuration unit, configured to, in response to detecting the occurrence of an event, configure a corresponding coroutine for the event; The coroutine scheduling unit is configured to, in response to detecting that the event data of the event is in a readable state, schedule the coroutine to process the event data, including: in response to detecting that the event data of the event is in a readable state, scheduling the coroutine to process the event data, and detecting whether a new event occurs, and / or the reading status of the event data of an event that has occurred in a non-blocking manner.
9. An electronic device comprising: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor. The instructions are executed by the at least one processor to enable the at least one processor to perform the method for processing an event according to any one of claims 1 to 7.
10. A non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to cause the computer to execute the method for processing an event according to any one of claims 1 to 7.
11. A computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements the method for processing an event according to any one of claims 1 to 7.
Citation Information
Patent Citations
Coroutine implementation method, terminal device and storage medium
CN108021449A
Data processing method, device and equipment and storage medium
CN111078436A