Coroutine-based high-performance data acquisition method and system

By introducing a coroutine scheduling framework into the data acquisition system, asynchronous non-blocking network communication is achieved, the problem of performance bottlenecks of data acquisition system in the existing technology is solved, efficient data acquisition and processing is achieved, and a large number of concurrent requests are supported.

CN118972266APending Publication Date: 2024-11-15SHANGHAI PLATFORM FOR SMART MFG CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202311412197.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-10-27
Publication Date
2024-11-15

AI Technical Summary

Technical Problem

The existing blocking system based on threads and processes cannot meet high-performance requirements, especially when it is necessary to monitor a large number of data acquisition terminals at the same time, it cannot effectively handle concurrent requests, resulting in performance bottlenecks.

Method used

Adopt a high-performance data acquisition system based on coroutines, and realizes asynchronous and non-blocking network communication through coroutine scheduling framework. Using TCP/IP protocol and PHP Swoole programming framework, ensures that the program is not blocked during network requests and automatically switches to another. Coroutine tasks to improve the efficiency of data acquisition and processing.

Benefits of technology

It realizes high-performance data acquisition and processing that supports hundreds of thousands of concurrent requests, avoids memory consumption and CPU switching resources of the operating system scheduling threads, and improves system performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118972266A_ABST
    Figure CN118972266A_ABST
Patent Text Reader

Abstract

The invention discloses a coroutine-based high-performance data acquisition method and system, which are used for realizing coroutine scheduling without CPU (Central Processing Unit) resources through a coroutine framework scheduling configuration program so as to establish an asynchronous non-blocking network. By adopting a coroutine method, a callback function mechanism can be packaged in a coroutine scheduling framework, only direct requests are needed in a user program, the coroutine scheduling framework ensures that after the requests are initiated, other coroutine tasks are immediately switched to be executed, and after the requests are completed, the coroutine is returned to continue to execute the tasks downwards, so that the task execution efficiency is improved. And obscure and difficulty caused by using a callback function layer by layer in a user program are overcome. And high-performance data acquisition and processing are realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to a data acquisition system and a data acquisition method, and in particular to a high-performance data acquisition method and an acquisition system based on coroutine control. Background Art

[0002] In current industrial data collection scenarios, it is generally necessary to monitor a large amount of field sensor data at the same time. Some application sites even need to monitor tens of thousands of data collection terminals at the same time to reproduce and process the production line situation. Current data collection systems all use servers to connect data collection terminals and data usage terminals at the same time, so that the server processes and sends the data collected by the data collection terminal to the data usage terminal, and processes and displays it at the data usage terminal. However, the data acquisition and transmission system based on threads and processes is a blocking system, that is, the server can only process a set of data at the same time, and can only implement polling processing for multiple concurrent events. When an event is running, it can only wait at other times, so polling and waiting are required. The same server will always be blocked when processing data transmission. Conventional multi-process or multi-thread plus synchronous blocking methods can often only support hundreds to thousands of concurrent requests, which cannot meet high performance requirements.

[0003] When the data collection requirements are increased, for example, data is collected from tens of thousands of terminals per second (or similar intensity, such as polling hundreds of sensors every 10 milliseconds), and sent to the data consumer program, or detect whether the data has changed, and timely perceive and notify the data consumer program when the data changes. If non-blocking asynchronous request coroutine control can be adopted, and tasks can be scheduled in the user program by itself, memory consumption when the operating system schedules threads or processes and CPU switching resources, lock competition, etc. can be avoided, the same system can generally support at least hundreds of thousands of concurrent requests, and realize data processing of multiple collection terminals and multiple use terminals. However, there are currently no data collection systems and methods that adopt coroutine control in industrial scenarios. Summary of the invention

[0004] The purpose of the present invention is to provide a high-performance data acquisition system based on coroutines, and at the same time to provide a data acquisition method used in the above system, so as to improve the data acquisition processing speed and system performance.

[0005] The high-performance data acquisition system based on coroutine of the present invention comprises a data acquisition module, a data acquisition service module and a data consumption module. The data acquisition module, the data acquisition service module and the data consumption module establish communication with each other using TCP / IP protocol. The data acquisition module comprises a data acquisition sensor and a preliminary data acquisition processing program; the data acquisition service module comprises a data acquisition server and a data acquisition configuration scheduling program; the data consumption module comprises a data processing terminal and a data terminal processing program.

[0006] In the above-mentioned high-performance data acquisition system based on coroutines, the sensor is a sensor that supports a data acquisition protocol based on TCP / IP, including a sensor that supports the modbus tcp protocol, a sensor that supports the snap7 protocol, or a sensor that supports the opcua communication protocol, to support the configuration of a coroutine scheduling framework.

[0007] In the above-mentioned high-performance data acquisition system based on coroutine, the sensor is a sensor that supports TCP / IP network acquisition protocol through PLC or expansion module. The expansion module is used to realize the support of other sensors for TCP / IP network acquisition protocol.

[0008] In the above-mentioned high-performance data acquisition system based on coroutines, the data acquisition service module includes a programming language or programming framework that supports coroutine scheduling; the coroutine scheduling framework ensures that the program is not blocked when a network request is made, and automatically switches to another coroutine task. The coroutine programming framework is the basis of the coroutine scheduler, and the operation of the asynchronous non-blocking network is scheduled and configured with the coroutine scheduling framework.

[0009] In the above-mentioned high-performance data acquisition system based on coroutines, the coroutine scheduling framework is the swoole programming framework of the PHP programming language.

[0010] In the above-mentioned high-performance data acquisition system based on coroutine, the data consumption module data processing terminal is an industrial application processing terminal, including a rule diagnosis terminal, a data display terminal, and a database terminal.

[0011] A high-performance data acquisition method based on a coroutine of the present invention comprises the following process:

[0012] A: The data acquisition service module loads all sensor configurations to be collected through the main program, establishes a TCP / IP communication connection for it, and creates a data acquisition coroutine;

[0013] B: Call the non-blocking version of the API to send a non-blocking data collection request to the sensor, establish a non-blocking data collection coroutine, and the sensor responds with a data collection response to collect relevant data;

[0014] C: The data collection coroutine determines the type of monitoring data. If it is ordinary data, a non-blocking data push request is established to transmit the data to the data consumption terminal; if it is a type that only monitors data changes, the next step is performed;

[0015] D: If it is a type that only monitors data changes, compare the data with the last time to see if there is any change. If there is no change, return the data collection request again through an asynchronous non-blocking request; if there is a change, execute data push and transmit the data to the data consumption terminal.

[0016] In the above-mentioned high-performance data acquisition method based on coroutine, the data acquisition coroutine execution process includes the following steps:

[0017] a: The coroutine scheduler creates data collection coroutine 1. After data collection coroutine 1 initiates a network connection request, the coroutine scheduler can execute other coroutines. After the network connection is successful, it returns to execute coroutine 1 to send network data.

[0018] b: When coroutine 1 is sending network data, it can switch to executing other coroutines. After coroutine 1 finishes sending network data, the coroutine scheduler switches to executing coroutine 1 and the execution switches to receiving network data. During this process, the coroutine scheduler executes other coroutines again.

[0019] c: After receiving the network data, the coroutine scheduler switches to executing coroutine 1 and starts closing the network connection. During the closing operation, the coroutine scheduler switches to executing other coroutines until the network connection is closed in coroutine 1 and then returns to coroutine 1.

[0020] The high-performance data acquisition and processing system and method based on coroutines in the present invention implements coroutine scheduling without CPU resources through the coroutine framework scheduling configuration program, thereby forming an asynchronous non-blocking network. By adopting the coroutine method, the callback function mechanism can be encapsulated in the coroutine scheduling framework. Only a direct request is required in the user program. The coroutine scheduling framework ensures that after the request is initiated, it immediately switches to other coroutine tasks for execution. After the request is completed, it returns to the coroutine to continue to execute the task downward, overcoming the obscurity caused by the use of layers of callback functions in the user program. High-performance data acquisition and processing is achieved. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1 It is a structural diagram of a high-performance data acquisition system based on coroutines of the present invention;

[0022] Figure 2 It is a flow chart of the high-performance data acquisition method based on coroutine of the present invention;

[0023] Figure 3 It is a flowchart of high-performance data acquisition coroutine scheduling based on coroutine of the present invention; DETAILED DESCRIPTION

[0024] The following is a description of the high-performance data acquisition system and method based on coroutines of the present invention in conjunction with the accompanying drawings and specific embodiments. The specific drawings and embodiments are only used for illustration and are not intended to limit the scope of protection.

[0025] The high-performance data acquisition system based on coroutines in this embodiment is composed of a data acquisition module, a data acquisition service module and a data consumption module. The data acquisition module, the data acquisition service module and the data consumption module establish communication with each other using the TCP / IP protocol. The data acquisition module includes a data acquisition sensor and a preliminary data acquisition processing program; the data acquisition service module includes a data acquisition server and a data acquisition configuration scheduling program; the data consumption module includes a data processing terminal and a data terminal processing program.

[0026] like Figure 1 As shown, in the system of this embodiment, the data acquisition service module is the core of the system, which is connected to most sensors by wire or wireless. These sensors can be various sensors, which are used to collect various data in industrial scenes. The sensor prefabricated data acquisition and preliminary processing software supports TCP / IP transmission protocol. For example, modbus tcp, Siemens snap7, opcua, etc. For sensors that do not support this protocol, network acquisition can also be supported through PLC or expansion modules.

[0027] The data acquisition service module includes a data acquisition server hardware system and a software system, and the overall data acquisition service module needs to support a programming language or programming framework that supports coroutine scheduling, so as to facilitate the establishment of an asynchronous non-blocking network in this embodiment. In this embodiment, the swoole framework of the PHP language is used as an example. The coroutine scheduling framework can ensure that the program is not blocked when a network request is made, and can automatically switch to another coroutine task.

[0028] The data consumption module is the back-end service of various industrial applications, that is, the data usage terminal, which also includes the data terminal hardware system and software system, such as a detection terminal that diagnoses whether the data is abnormal according to various rules; it can also be a data display terminal that displays the data on a large data screen in real time; in particular, it can also be a database service, so that the data can be directly stored in the server.

[0029] The data acquisition module and the data acquisition service module establish a wired or wireless connection using the TCP / IP communication protocol, and the data acquisition service module and the data consumption module establish a wired or wireless TCP / IP communication connection, thereby ensuring the establishment and communication of the asynchronous non-blocking network system in this embodiment.

[0030] The data collection and processing process of the system in this embodiment is as follows: Figure 2 shown.

[0031] First, when the data collection service is started, all sensor configurations to be collected are loaded, such as data name, collection protocol, collection address, data type, etc. However, there is a specific requirement that the IO calls (such as sending and receiving network requests, reading and writing files, etc.) and waiting operations of the data collection service should be non-blocking, that is, the asynchronous non-blocking version of the corresponding API must be called. Therefore, each coroutine is scheduled by the coroutine framework. Once a non-blocking asynchronous operation is encountered, the control is handed over to the scheduler until the asynchronous operation is completed, and then the execution continues.

[0032] Next, the data collection service module issues a non-blocking data collection request and sends the request to the sensor of the specific data collection module. The sensor executes the data collection instruction and sends it back to the data collection service module for identification and processing. If the data collection service module determines that the data is only collected by the data collection instruction, it will issue a non-blocking data push request and push the data information to the data consumption module. The data consumption module processes the data, displays it, diagnoses it, or enters it into the database.

[0033] When the data collection service module determines that the data is collected only to detect data changes, the data collection service module compares the collected data with the last corresponding data. If it finds that the data has changed, it transmits the changed data information to the data consumption module through a non-blocking request for data diagnosis or other processing.

[0034] When the data collection service module determines that the data has not changed, it will not transmit data to the data consumption module, but will feedback the data collection request and continue to send data collection instructions. Only after the data has changed will it transmit data to the data consumption module in a non-blocking request manner.

[0035] In the data collection service, for each sensor, the coroutine scheduler is as follows Figure 3 shown.

[0036] For each sensor, the data collection service will start a coroutine for scheduled data collection. The method for starting a coroutine is provided by the coroutine framework, such as the go function provided by the swoole framework. After entering the coroutine, a TCP request is initiated according to the collection protocol. Note that the API interface that supports asynchronous requests in the coroutine framework is used. For example, in the swoole framework, the built-in API function fsockopen in the PHP language can be used directly to open the network connection and fread / fwrite can be used to enter the reading and writing. These APIs have been automatically replaced with versions that support asynchronous requests when the coroutine framework is initialized.

[0037] When the asynchronous request is completed, the collected data is returned. At this time, the data collection service opens another coroutine to initiate communication with the data consumption program based on the TCP / IP protocol.

[0038] Specifically, one step closer.

[0039] First, the data acquisition service module creates a coroutine for a specific sensor 1, which provides data acquisition coroutine services for sensor 1. The coroutine initiates a network connection between sensor 1 and the data acquisition service module and the data consumption module, and can provide network data transmission services for sensor 1. After the network connection is successful, it receives network data. At the same time, other coroutines can also be scheduled for execution, such as data acquisition service coroutine 2 of sensor 2. After the network data reception of sensor 1 is completed, the network connection of sensor 1 is closed, and other coroutines can be executed again, such as coroutine N operation of sensor N. That is, during the execution of coroutine sensor 1, it will switch to executing other coroutine operations according to specific needs, thereby realizing non-blocking asynchronous request operations.

[0040] In the asynchronous I / O model of this embodiment, the coroutine is more suitable for I / O intensive application scenarios and can better utilize resources. Only one coroutine can run at the same time, such as coroutine 1. Once the coroutine executes an I / O operation or actively gives up control, it will switch to other coroutines for execution, such as coroutines. The coroutine in the PHP language needs to use the yield keyword, which means that the coroutine yields, gives up control, and waits for the execution of other coroutines or the triggering of I / O events.

[0041] As a green thread, the Swoole coroutine scheduler runs in user mode and can achieve multi-tasking scheduling and high-performance network applications by switching coroutines and scheduling thread resources very efficiently. The coroutine scheduler creates a coroutine stack (coroutine_stack) each time it executes, and saves the context to the coroutine stack, and reloads the context when the coroutine needs to resume execution.

[0042] The scheduling method of the coroutine scheduler is also non-preemptive. Only when it is possible to switch to other coroutines for execution will the current coroutine take the initiative to give up control and the scheduler will perform the coroutine switching operation.

[0043] Swoole's coroutine scheduler wraps the underlying network I / O with coroutines to achieve the effect of coroutine switching, rather than true multithreading. Swoole uses Epoll or Kqueue at the bottom layer for event polling and I / O multiplexing, in conjunction with the coroutine switching mechanism.

[0044] The Swoole coroutine scheduler implements coroutine scheduling through three steps.

[0045] 1. Coroutine creation: When the Swoole program starts executing, the coroutine scheduler starts working. When the coroutine is created, the coroutine scheduler assigns a coroutine_id to represent the ID of the coroutine, and then opens up a space to store the context information of the coroutine, including the currently executed file, the current line number, etc., and then saves this context information to a coroutine_stack data structure.

[0046] 2. Coroutine switching

[0047] The coroutine scheduler is responsible for switching between multiple coroutines. Once a coroutine is activated or needs to wait for other events, the coroutine scheduler will switch to another coroutine for execution. This switching process seems very cumbersome. In fact, the Swoole coroutine scheduler has been optimized. It only needs to save the context to the current running stack, and then load the context from the running stack of another coroutine to switch the coroutine.

[0048] 3. Coroutine recycling

[0049] After the coroutine ends, the memory needs to be released in time. The coroutine scheduler recycles by detecting the usage of the coroutine stack. If the internal memory in the coroutine stack is not occupied, it means that the coroutine can be recycled. When recycling, the context of the coroutine will be removed from the coroutine scheduler first, and then the memory in the coroutine stack will be destroyed, and the underlying file descriptors and other resources will be released.

[0050] The system and method of the present invention implements coroutine scheduling without CPU resources through the coroutine framework scheduling configuration program, thereby forming an asynchronous non-blocking network. By adopting the coroutine method, the callback function mechanism can be encapsulated in the coroutine scheduling framework. Only a direct request is needed in the user program. The coroutine scheduling framework ensures that after the request is initiated, it immediately switches to other coroutine tasks for execution. After the request is completed, it returns to the coroutine to continue to execute the task downward, overcoming the obscurity caused by the use of layers of callback functions in the user program. High-performance data collection and processing is achieved.

Claims

1. A high-performance data acquisition system based on coroutines, including a data acquisition module, a data acquisition service module and a data consumption module. Features: Network communication is established between the data acquisition module, the data acquisition service module and the data consumption module using the TCP / IP protocol; the data acquisition module includes a data acquisition sensor and a data acquisition processing program; the data acquisition service module includes a data acquisition server and a data acquisition configuration scheduling program; the data consumption module includes a data processing terminal and a data terminal processing program.

2. The high-performance data acquisition system based on coroutine according to claim 1, Features: The sensor is a sensor that supports a data acquisition protocol based on TCP / IP, including one or a combination of a sensor that supports a modbus tcp protocol, a sensor that supports a snap7 protocol, or a sensor that supports an opcua communication protocol.

3. The high-performance data acquisition system based on coroutine according to claim 1, Features: The sensor is a sensor that supports the TCP / IP network acquisition protocol through a PLC or an expansion module.

4. The high-performance data acquisition system based on coroutine according to claim 1, Features: The data acquisition service module includes a programming language or a programming framework that supports coroutine scheduling; the coroutine scheduling framework ensures that the program is not blocked when a network request is made and automatically switches to another coroutine task.

5. The high-performance data acquisition system based on coroutine according to claim 4, Features: The coroutine scheduling framework is the swoole programming framework of the php programming language.

6. The high-performance data acquisition system based on coroutine according to claim 1, Features: The data consumption module data processing terminal is an industrial application processing terminal, including a rule diagnosis terminal, a data display terminal or a database terminal.

7. A high-performance data acquisition method based on coroutines, Features The process includes: A: The data acquisition service module loads all sensor configurations to be collected through the main program, establishes a TCP / IP communication connection for it, and creates a data acquisition coroutine; B: Call the non-blocking version of the API to send a non-blocking data collection request to the sensor, establish a non-blocking data collection coroutine, and the sensor responds with a data collection response to collect relevant data; C: The data collection coroutine determines the type of monitoring data. If it is ordinary data, a non-blocking data push request is established to transmit the data to the data consumption terminal; if it is a type that only monitors data changes, the next step is performed; D: If it is a type that only monitors data changes, compare the data with the last time to see if there is any change. If there is no change, return the data collection request again through an asynchronous non-blocking request; if there is a change, execute data push and transmit the data to the data consumption terminal.

8. The high-performance data acquisition method based on coroutine according to claim 7, Features: The data acquisition coroutine execution process includes the following steps: a: The coroutine scheduler creates data collection coroutine 1. After data collection coroutine 1 initiates a network connection request, the coroutine scheduler can execute other coroutines. After the network connection is successful, it returns to execute coroutine 1 to send network data. b: When coroutine 1 is sending network data, it can switch to executing other coroutines. After coroutine 1 finishes sending network data, the coroutine scheduler switches to executing coroutine 1 and the execution switches to receiving network data. During this process, the coroutine scheduler executes other coroutines again. c: After receiving the network data, the coroutine scheduler switches to executing coroutine 1 and starts closing the network connection. During the closing operation, the coroutine scheduler switches to executing other coroutines until the network connection is closed in coroutine 1 and then returns to coroutine 1.

Citation Information

Cited By

  • SOME / IP network service state monitoring system and method

    CN122339999A

  • Some / ip network service state monitoring system and method

    CN122339999B