Task processing method and related device

By creating a second thread in the Rust language to handle asynchronous operations and utilizing the event loop mechanism, the problem of low execution efficiency caused by time-consuming tasks in the Rust language is solved, and the system throughput and performance are improved.

CN120653377APending Publication Date: 2025-09-16TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410309557.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-03-15
Publication Date
2025-09-16

AI Technical Summary

Technical Problem

In the Rust language, time-consuming tasks such as network requests and timer operations lead to low execution efficiency and inefficient use of execution stack resources.

Method used

Based on the execution stack of the Rust language, a second thread is created to handle asynchronous operations, and the asynchronous tasks are added to the task queue after the response time ends. The event loop mechanism is used to execute asynchronous tasks when the execution stack is idle, ensuring the processing efficiency of synchronous tasks.

Benefits of technology

Improves the system's throughput and performance, ensuring efficient processing of synchronous tasks while simultaneously handling the response time of multiple asynchronous operations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120653377A_ABST
    Figure CN120653377A_ABST
Patent Text Reader

Abstract

The invention discloses a task processing method and a related device, and is applied to the technical field of computers. The method comprises the following steps: generating a synchronization task for a synchronization operation which does not need response time, adding the synchronization task to an execution stack constructed based on a rust language, continuously taking out the task from the execution stack for execution through a first thread according to an event circulation mechanism, and when an asynchronous operation which needs to wait for response time is obtained, executing the task through a second thread. When the asynchronous operation is executed, a second thread background can be created to wait for the response time of the asynchronous operation, asynchronous tasks can be generated and arranged in a task queue when the response mission is finished, and when an execution stack is empty, the asynchronous tasks can be added from the task queue and then executed according to an event circulation mechanism. By means of the mode, on the basis of the rust language execution stack, the second thread is created to execute the asynchronous operation, a program can process response time of multiple asynchronous operations at the same time in the background, the throughput and performance of the system are improved, the asynchronous task continues to be executed when the execution stack is idle, and the processing efficiency of the synchronous task is guaranteed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a task processing method and related devices. Background Art

[0002] A thread is a term used in operating system technology. It is the smallest unit of computational scheduling within an operating system. It is contained within a process and serves as the actual operational unit within that process. A thread refers to a single, sequential flow of control within a process. A process can have multiple threads running in parallel, each executing different tasks. A single-threaded process has only one thread. When a single-threaded program executes, the program path is arranged sequentially; the preceding thread must be processed before the following thread can be executed.

[0003] The system-level programming language Rust runs in a single-threaded model, which means that it executes one task at a time. These tasks form an execution stack and queue up for execution. However, some front-end tasks are very time-consuming, such as network requests, timers, and event monitoring. They need to wait for the response time before execution. If these tasks are queued for execution, the execution efficiency of these tasks will be very low. Summary of the Invention

[0004] The embodiments of the present application provide a task processing method and related devices. Based on the rust language execution stack, a second thread is created to perform asynchronous operations. The program can simultaneously process the response time of multiple asynchronous operations in the background, improve the throughput and performance of the system, and continue to execute asynchronous tasks when the execution stack is idle, thereby ensuring the processing efficiency of synchronous tasks.

[0005] In view of this, the present application provides a task processing method, including:

[0006] Get synchronization operations and generate synchronization tasks in the Rust-based execution stack. Synchronous operations are operations without response time. The execution stack is used to execute tasks that are executed immediately.

[0007] The first thread takes out tasks from the execution stack based on the event loop mechanism and executes them. The event loop mechanism is used to execute the tasks queued in the execution stack in sequence.

[0008] Get asynchronous operations, which are operations with response time;

[0009] Create a second thread to perform asynchronous operations and generate asynchronous tasks in the task queue after the response time;

[0010] When the execution stack is empty, the asynchronous task is extracted from the task queue and added to the execution stack.

[0011] Another aspect of the present application provides a task processing device, comprising:

[0012] The acquisition unit is used to obtain synchronization operations and generate synchronization tasks in the Rust-based execution stack. Synchronous operations are operations without response time, and the execution stack is used to execute tasks that are executed immediately;

[0013] An execution unit is configured to retrieve tasks from an execution stack for execution in the first thread based on an event loop mechanism, wherein the event loop mechanism is configured to sequentially execute tasks queued in the execution stack;

[0014] The acquisition unit is further used to acquire asynchronous operations, where the asynchronous operations are operations with response time;

[0015] The execution unit is further configured to create a second thread to execute an asynchronous operation and generate an asynchronous task in a task queue after a response time; and extract an asynchronous task from the task queue and add it to the execution stack when the execution stack is empty.

[0016] In a possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the execution unit is specifically configured to:

[0017] Determine execution priority for multiple asynchronous operations;

[0018] Add the asynchronous task corresponding to the asynchronous operation to the task queue. The asynchronous task includes an execution priority. The asynchronous task with a high execution priority is executed first in the execution stack.

[0019] In a possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the execution unit is specifically configured to:

[0020] The execution priorities of multiple asynchronous operations are divided based on their creation time.

[0021] In a possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the execution unit is specifically configured to:

[0022] The calling thread pool executes the tasks in the execution stack in parallel based on the event loop mechanism, and the threads of the thread pool include the first thread.

[0023] In one possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the task processing device further includes a sleep unit, and the sleep unit is specifically configured to:

[0024] When the thread pool resources are full, the event loop mechanism is dormant;

[0025] When new resources are released in the occupied thread pool, the event loop mechanism is awakened.

[0026] In a possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the execution unit is specifically configured to:

[0027] When the execution stack is empty, extract the asynchronous task from the task queue;

[0028] Add the callback function corresponding to the asynchronous task to the execution stack.

[0029] In a possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the execution unit is further configured to:

[0030] The first thread reads the callback function from the execution stack based on the event loop mechanism;

[0031] Call the callback function to perform asynchronous tasks.

[0032] In one possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the task processing device further includes a sleep unit, and the sleep unit is specifically configured to:

[0033] When both the execution stack and the task queue are empty, the event loop mechanism is dormant;

[0034] When the execution stack or task queue is not empty and the event loop mechanism is dormant, wake up the event loop mechanism.

[0035] In one possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the task processing device further includes a management unit, and the management unit is specifically configured to:

[0036] Use Box pointers to manage synchronous and asynchronous tasks.

[0037] In one possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the task processing device further includes a management unit, and the management unit is specifically configured to:

[0038] Arc based on Rust shares synchronous and asynchronous tasks between multiple threads.

[0039] In a possible implementation manner, in another implementation manner of another aspect of the embodiment of the present application, the management unit is further configured to:

[0040] Rust-based Mutex is used to lock access to shared synchronous and asynchronous tasks.

[0041] Another aspect of the present application provides a computer device, comprising:

[0042] memories, transceivers, processors, and bus systems;

[0043] Wherein, the memory is used to store programs;

[0044] The processor is used to execute the program in the memory, including executing the above-mentioned methods;

[0045] The bus system is used to connect the memory and the processor so that the memory and the processor can communicate with each other.

[0046] Another aspect of the present application provides a computer-readable storage medium, wherein instructions are stored in the computer-readable storage medium. When the computer-readable storage medium is run on a computer, the computer is enabled to execute the above-mentioned methods.

[0047] Another aspect of the present application provides a computer program product or computer program, which includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the methods provided in the above aspects.

[0048] It can be seen from the above technical solutions that the embodiments of the present application have the following advantages:

[0049] The embodiment of the present application generates a synchronous task for a synchronous operation that does not require a response time and adds it to an execution stack built based on the rust language. Then, according to the event loop mechanism, the first thread continuously takes out tasks from the execution stack in sequence for execution. When an asynchronous operation that needs to wait for a response time is obtained, a second thread can be created to wait for the response time of the asynchronous operation in the background. When the response time is completed, an asynchronous task can be generated and arranged in a task queue. When the execution stack is empty, an asynchronous task can be added from the task queue and then executed according to the event loop mechanism. In the above manner, based on the rust language execution stack, a second thread is created to execute asynchronous operations. The program can process the response time of multiple asynchronous operations at the same time in the background, improve the throughput and performance of the system, and continue to execute asynchronous tasks when the execution stack is idle, thereby ensuring the processing efficiency of synchronous tasks. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] Figure 1 This is a block diagram of an exemplary system architecture in an embodiment of the present application;

[0051] Figure 2 A flowchart of a task processing method in an embodiment of the present application is shown;

[0052] Figure 3 This is a processing flow chart of synchronous operation and asynchronous operation in an embodiment of the present application;

[0053] Figure 4 This is a processing sequence diagram of synchronous operation and asynchronous operation in an embodiment of the present application;

[0054] Figure 5 This is a structural diagram of a task processing device in an embodiment of the present application;

[0055] Figure 6 This is a structural diagram of a computer device in an embodiment of the present application. DETAILED DESCRIPTION

[0056] The embodiments of the present application provide a task processing method and related devices. Based on the rust language execution stack, a second thread is created to perform asynchronous operations. The program can simultaneously process the response time of multiple asynchronous tasks in the background, improve the throughput and performance of the system, and continue to execute asynchronous tasks when the execution stack is idle, thereby ensuring the processing efficiency of synchronous tasks.

[0057] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequential order. It should be understood that the numbers used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can, for example, be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "corresponding to" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0058] The word “exemplary” is used exclusively herein to mean “serving as an example, example, or illustration.” Any embodiment described herein as “exemplary” is not necessarily to be construed as preferred or advantageous over other embodiments.

[0059] In the embodiments of the present application, the term "module" or "unit" refers to a computer program or a part of a computer program that has a predetermined function and works together with other related parts to achieve a predetermined goal, and can be implemented in whole or in part by using software, hardware (such as processing circuits or memories) or a combination thereof. Similarly, a processor (or multiple processors or memories) can be used to implement one or more modules or units. In addition, each module or unit can be part of an overall module or unit that includes the function of the module or unit.

[0060] In addition, numerous specific details are provided in the detailed description below to better illustrate the present application. Those skilled in the art will appreciate that the present application can be practiced without certain specific details. In some instances, methods, means, components, and circuits well known to those skilled in the art are not described in detail in order to highlight the main purpose of the present application.

[0061] For ease of understanding, the terms involved in the embodiments of this application are explained below.

[0062] A thread is the smallest unit that the operating system can use to schedule operations. It is contained in a process and is the actual operating unit in the process. A thread refers to a single sequential control flow in a process. A process can have multiple threads running concurrently, and each thread executes different tasks in parallel. When a program starts, a process is created by the operating system, and at the same time, a thread starts running immediately. This thread is usually called the main thread of the program. Every process has at least one main thread.

[0063] Rust is a systems-level programming language designed to provide a good balance of security, concurrency, and performance, with memory safety and data race freedom. Rust is designed to allow developers to write efficient, reliable, and concurrent software. It provides many features of modern programming languages, such as pattern matching, generics, an ownership system, lifecycle management, and modular organization. These features make Rust suitable for a variety of application scenarios, including systems programming, embedded development, network services, and web development. Rust is a single-threaded language, meaning it can only execute one task at a time.

[0064] Some tasks on the front end are very time-consuming, such as network requests, timers, and event monitoring, which require waiting for a response time before execution. If these tasks are queued for execution, the execution efficiency of these tasks will be very low.

[0065] An embodiment of the present application provides a task processing method, which generates a synchronous task for a synchronous operation that does not require a response time and adds it to an execution stack built based on the rust language. Then, according to the event loop mechanism, the first thread continuously takes out tasks from the execution stack in sequence for execution. When an asynchronous operation that needs to wait for a response time is obtained, a second thread can be created to wait for the response time of the asynchronous operation in the background. When the response is completed, an asynchronous task can be generated and arranged in a task queue. When the execution stack is empty, an asynchronous task can be added from the task queue, and then the asynchronous task can be executed according to the event loop mechanism. Through the above method, based on the rust language execution stack, a second thread is created to execute asynchronous operations. The program can process the response time of multiple asynchronous tasks at the same time in the background, improve the throughput and performance of the system, and continue to execute asynchronous tasks when the execution stack is idle, thereby ensuring the processing efficiency of synchronous tasks.

[0066] The embodiments of the present application are applied to the field of artificial intelligence (AI). Artificial intelligence is the theory, method, technology, and application system for using digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use knowledge to achieve optimal results. In other words, artificial intelligence is a comprehensive technology in computer science that attempts to understand the essence of intelligence and produce new intelligent machines that can respond in a manner similar to human intelligence. Artificial intelligence is the study of the design principles and implementation methods of various intelligent machines, giving them the capabilities of perception, reasoning, and decision-making.

[0067] Artificial intelligence (AI) technology is a comprehensive discipline encompassing a wide range of fields, encompassing both hardware and software technologies. Foundational AI technologies generally include sensors, specialized AI chips, cloud computing, distributed storage, big data processing, pre-trained models, operating / interaction systems, and mechatronics. Pre-trained models, also known as large models or basic models, can be fine-tuned and widely applied to downstream tasks across various AI disciplines. AI software technologies primarily encompass computer vision, speech processing, natural language processing, and machine learning / deep learning.

[0068] The data analysis method provided in the embodiment of the present application can be implemented by various electronic devices, for example, it can be implemented by a terminal device alone, or it can be implemented by a server and a terminal device in collaboration. For example, the terminal device executes the data analysis method described below alone, or the terminal device and the server jointly execute the data analysis method described below, for example, the terminal device obtains a synchronous operation to be executed immediately, notifies the synchronous operation to the server, and the server adds a synchronous task corresponding to the synchronous operation to the execution stack based on rust. The server can continuously take out tasks from the execution stack in sequence in the main thread based on the event loop mechanism. When the terminal device obtains an asynchronous operation that is not executed immediately, it also notifies the server of the asynchronous operation. The server creates a second thread to execute the asynchronous operation in the background. After the response time of the asynchronous operation ends, the corresponding asynchronous task is generated in the task queue. When the execution stack is empty, the asynchronous task can be taken out from the task queue and added to the execution stack for the event loop mechanism to take out and execute.

[0069] The electronic device for task processing provided in the embodiments of the present application can be various types of terminal devices or servers, wherein the server can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the terminal device can be a smart phone, tablet computer, laptop computer, desktop computer, smart speaker, smart watch, etc., but is not limited to this. The terminal device and the server can be directly or indirectly connected through wired or wireless communication, and the embodiments of the present application do not limit this.

[0070] Taking servers as an example, it can be a server cluster deployed in the cloud, opening artificial intelligence cloud services (AIaaS, AI as a Service) to objects. The AIaaS platform will split and execute several common AI services and provide independent or packaged services in the cloud. This service model is similar to an AI theme mall. All objects can access one or more artificial intelligence services provided by the AIaaS platform through the application programming interface.

[0071] The following is an example of the data analysis method provided by the embodiment of the present application being implemented in collaboration between a server and a terminal device. Figure 1 , Figure 1 1 is a schematic diagram of an exemplary system 100 provided in an embodiment of the present application. A terminal device 110 is connected to a server 130 via a network 120. The network 120 may be a wide area network (WAN) or a local area network (LAN), or a combination of the two.

[0072] In some embodiments, the terminal device 110 notifies the server of the acquired synchronous and asynchronous operations. The server 130 adds the synchronous task corresponding to the synchronous operation to the Rust-based execution stack, and the main thread loops to retrieve the task from the execution stack for execution. For the asynchronous operation, the server 130 creates a second thread to execute the asynchronous operation and adds the corresponding asynchronous task to the task queue after the response time corresponding to the asynchronous operation. When the execution stack is idle, the asynchronous task is retrieved from the task queue and added to the execution stack.

[0073] The task processing method provided in the embodiment of the present application will be described below with reference to the accompanying drawings. The execution subject of the following data analysis method takes the terminal device as an example, and can be specifically implemented by the terminal device by running the various computer programs mentioned above; of course, based on the understanding of the following text, it is not difficult to see that the task processing method provided in the embodiment of the present application can also be implemented collaboratively by the terminal device and the server.

[0074] The first thread in the embodiment of the present application may also be referred to as the main thread.

[0075] See also Figure 2 , Figure 2 FIG. 1 is a flow chart of a task processing method provided in an embodiment of the present application, the method comprising:

[0076] Step 201: Get a synchronization operation and generate a synchronization task in the Rust-based execution stack. The synchronization operation is an operation without response time. The execution stack is used to execute tasks that are executed immediately.

[0077] In this embodiment, the synchronization operation is an operation that is executed immediately. The synchronization task corresponding to the synchronization operation is pushed into the stack and executed immediately without waiting for the response time. The response time may be the time required to wait for certain operations or conditions to be met before the result is returned. The terminal device creates an execution stack in rust. When there is a synchronization operation, the terminal device can generate a synchronization task corresponding to the synchronization operation and add the synchronization task to the execution stack for queuing.

[0078] Step 202: In the first thread, based on the event loop mechanism, a task is retrieved from the execution stack for execution. The event loop mechanism is used to sequentially execute tasks queued in the execution stack.

[0079] In this embodiment, an infinite loop event loop mechanism is created in Rust. According to this event loop mechanism, the first thread continuously loops and detects tasks in the execution stack. When a task is detected, it is sequentially retrieved and executed in the order of the tasks. The execution method can be to retrieve tasks sequentially for execution. After the task is completed, the task is popped from the stack, and the event loop mechanism retrieves a new task from the execution stack for execution. For example, the execution stack has tasks 1, 2, and 3 queued for processing. The event loop mechanism can first retrieve task 1 for execution. After task 1 is executed, it is popped from the stack. The event loop mechanism then retrieves task 2 from the execution stack for execution, and then retrieves task 3 in sequence, and so on.

[0080] Step 203: Get an asynchronous operation, where the asynchronous operation is an operation with a response time.

[0081] In this embodiment, the terminal device needs to identify whether each operation obtained is a synchronous operation or an asynchronous operation. Asynchronous operations require waiting for a response time before processing. Processing asynchronous operations directly in the main thread requires waiting for a response time, which is likely to affect the processing efficiency of other tasks. Therefore, synchronous operations and asynchronous operations can be distinguished by identifying whether the operation requires a response time. Asynchronous operations can also be called time-consuming operations. For example, asynchronous operations can be timers, network requests, event monitoring, etc.

[0082] Step 204: Create a second thread to perform asynchronous operations, and generate an asynchronous task in the task queue after the response time.

[0083] In this embodiment, while the first thread, which is the main thread, continues to execute the synchronous task in the execution stack, a new thread is created to receive the asynchronous operation. The new thread can be called the second thread. The asynchronous operation can be processed in the second thread first, such as the waiting process for the response time of the asynchronous operation. When the processing is completed, the asynchronous task corresponding to the asynchronous operation can be added to the task queue.

[0084] In rust, the task queue can be implemented using VecDeque, or Vec can be used to simulate the execution stack.

[0085] Step 205: When the execution stack is empty, extract the asynchronous task from the task queue and add it to the execution stack.

[0086] In this embodiment, the first thread prioritizes synchronous tasks in the execution stack. Asynchronous tasks are not processed by the first thread until the first thread has completed all synchronous tasks. When the terminal device's execution engine is idle, that is, when tasks in the execution stack have completed execution, it can check the task queue for asynchronous tasks. If an asynchronous task is added to the task queue, it will add the asynchronous task to the execution stack. The event loop mechanism will then sequentially retrieve asynchronous tasks from the execution stack for execution.

[0087] In an embodiment of the present application, for synchronous operations that do not require response time, synchronous tasks are generated and added to an execution stack built based on the rust language. Then, according to the event loop mechanism, tasks are continuously taken out from the execution stack in sequence by the first thread for execution. When an asynchronous operation that needs to wait for a response time is obtained, a second thread can be created to wait for the response time of the asynchronous operation in the background. When the response time is completed, an asynchronous task can be generated and arranged in a task queue. When the execution stack is empty, an asynchronous task can be added from the task queue, and then the asynchronous task can be executed according to the event loop mechanism. In the above manner, based on the rust language execution stack, a second thread is created to execute asynchronous operations. The program can process the response time of multiple asynchronous operations at the same time in the background, improve the throughput and performance of the system, and continue to execute asynchronous tasks when the execution stack is idle, thereby ensuring the processing efficiency of synchronous tasks.

[0088] Optionally, in the above Figure 2 On the basis of the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, the method further includes:

[0089] Use Box pointers to manage synchronous and asynchronous tasks.

[0090] In one or more embodiments, a task memory management method is introduced. In Rust, a Box is a smart pointer allocated on the heap. Because the execution stack size is usually limited during program execution, using a Box can help avoid stack overflows. Its main function is to provide dynamic memory management for larger data structures or synchronous or asynchronous tasks that are not suitable for allocation on the execution stack.

[0091] In an embodiment of the present application, a task memory management method is provided. Through the above method, when there is a very large data structure or array and it is placed on the execution stack, it may cause an execution stack overflow. Using Box, these large data structures can be moved to the heap, thereby avoiding the execution stack overflow.

[0092] Optionally, in the above Figure 2 On the basis of the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, the method further includes:

[0093] Arc based on Rust shares synchronous and asynchronous tasks between multiple threads.

[0094] In one or more embodiments, a method for multiple threads to share data is introduced. Atomic reference counted (Arc) is an atomic reference counted smart pointer. Its main function is to provide a thread-safe shared ownership mechanism for data, allowing data of synchronous tasks and asynchronous tasks to be shared between multiple threads without worrying about data competition or life cycle issues. The use of Arc is based on a reference counting mechanism, which means that each time an Arc pointer is cloned, its internal reference count increases. When the Arc pointer goes out of scope or is explicitly discarded, the reference count decreases. Only when the reference count drops to zero will the data pointed to be released. This mechanism ensures the security of the data and avoids the complexity of manual memory management.

[0095] In this embodiment, a method for sharing data between multiple threads is provided. By using the above method, Arc in Rust provides a thread-safe reference counting smart pointer, making it easier and safer to share data between multiple threads.

[0096] Optionally, in the above Figure 2 On the basis of the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, the method further includes:

[0097] Rust-based Mutex is used to lock access to shared synchronous and asynchronous tasks.

[0098] In one or more embodiments, a multi-thread access method is introduced. Mutex (mutual exclusion lock) is a synchronization primitive used to protect shared resources and ensure that only one thread can access shared resources at any time, such as only one thread can access the data of synchronous tasks and asynchronous tasks shared by Arc among multiple threads. When multiple threads need to access and modify the same data, using a mutex lock can avoid data contention and inconsistency. It can be understood that a mutex lock achieves synchronization between threads through a locking mechanism. When a thread wants to access shared data protected by a mutex lock, it must first acquire the mutex lock. If the lock is already held by other threads, the thread will be blocked until the lock is released. Once a thread obtains the lock, it can safely access and modify shared data without worrying about interference from other threads.

[0099] For example, in Rust, we can use VecDeque to implement the task queue and use Vec to simulate the main thread's execution stack. In the above code, we create an EventLoop structure for the event loop, which contains a main thread's execution stack and a task queue for asynchronous tasks. Among them, we can also define some methods to operate on these two data structures:

[0100] The push_stack and pop_stack methods are used to add and remove tasks to the main thread's stack.

[0101] The enqueue_task and dequeue_task methods are used to add and remove tasks to the asynchronous task queue.

[0102] The Task type here is a dynamically dispatched closure that can store any callable object. We use Box to allocate this closure on the heap and use dyn FnOnce() + Send to indicate that this closure can be called once and can be passed between multiple threads. The code for implementing the task queue and execution stack in Rust can be found below:

[0103]

[0104]

[0105] In an embodiment of the present application, a multi-thread access method is provided. By using a mutex lock to protect shared data from being accessed by multiple threads simultaneously, developers can safely share and access data in a multi-threaded environment.

[0106] Optionally, in the above Figure 2 On the basis of the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, the method further includes:

[0107] When both the execution stack and the task queue are empty, the event loop mechanism is dormant;

[0108] When the execution stack or task queue is not empty and the event loop mechanism is dormant, wake up the event loop mechanism.

[0109] In one or more embodiments, a method for dormant event loop mechanism is introduced. Since the event loop mechanism continuously reads tasks from the execution stack, when the execution stack is empty, the event loop mechanism can take out asynchronous tasks from the task queue and add them to the execution stack. When there are no tasks in the execution stack and the task queue, the event loop mechanism cannot read tasks from the execution stack, but still performs the read operation. The embodiment of the present application can put the event loop mechanism to sleep when there is no event processing, that is, when the execution stack and the task queue are both empty, to avoid meaningless read operations. Only when an event occurs, that is, when either the execution stack or the task queue is not empty, the event loop mechanism will be awakened and the corresponding event processing function will be called to process the event.

[0110] For example, in the run method, we create an infinite loop that repeatedly removes tasks from the main thread's execution stack and executes them. When the main thread's execution stack is empty, it removes tasks from the asynchronous task's task queue and adds them to the main thread's execution stack. We use the while let syntax to attempt to remove tasks from both the main thread's execution stack and the asynchronous task's task queue. If the task removed is Some(task), we execute it or add it to the main thread's execution stack. If the task removed is None, we exit the loop. We use the multi-producer single-consumer (mpsc) channel from the std::sync::mpsc module to handle asynchronous tasks. We create a new thread to receive asynchronous tasks and add them to the task queue. We also add an add_task method that accepts a task and a sender and sends the asynchronous task to the channel. In the run method, we add a check to see if both the main thread's execution stack and the asynchronous task's task queue are empty. If both are empty, we temporarily sleep the event loop to avoid unnecessary CPU usage. The code implementing the event loop mechanism in Rust is shown below:

[0111]

[0112]

[0113] In an embodiment of the present application, a method for dormant event loop mechanism is provided. By the above method, the event loop mechanism is dormant when there is no event to be processed, and is awakened only when an event occurs, thereby reducing the meaningless resource occupation of the event loop mechanism.

[0114] Optionally, in the above Figure 2 On the basis of the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, after creating the second thread to perform the asynchronous operation, the method further includes:

[0115] Determine execution priority for multiple asynchronous operations;

[0116] Add the asynchronous task corresponding to the asynchronous operation to the task queue. The asynchronous task includes an execution priority. The asynchronous task with a high execution priority is executed first in the execution stack.

[0117] In one or more embodiments, a method for executing asynchronous tasks is introduced. Asynchronous tasks are divided into many types, such as file input and output (IO), network IO, timers, etc. These different asynchronous operations may end at the same event. At this time, when multiple asynchronous tasks are generated at the same time, there will be a question of which asynchronous task will be processed first, which will be executed first and which will be executed later. The embodiment of the present application can configure an execution priority for each asynchronous operation, and equip the execution priority to the asynchronous task corresponding to the asynchronous operation. The execution order of the asynchronous tasks is first arranged, and then when multiple asynchronous operations end at the same event, they can be executed in this order. Correspondingly, the asynchronous tasks in the execution stack also have execution priorities, and the event loop mechanism gives priority to executing asynchronous tasks with higher execution priorities from the execution stack. For example, assuming there are two asynchronous tasks in the execution stack, one with an execution priority of 1 and the other with an execution priority of 2, the asynchronous task with execution priority 2 will be taken out first for execution.

[0118] For example, in actual development, our asynchronous tasks are divided into many types, such as file IO, network IO, timers, etc. These different asynchronous tasks may end at the same event. At this time, we need to execute them in order of priority. In the above code, we define a PrioritizedTask (task with priority) structure, which contains a priority and a task. We implement the PartialEq (partial equality), Eq (equality), PartialOrd (partial order), and Ord (total order) traits, so that we can store PrioritizedTask in a BinaryHeap (binary heap).

[0119] We use BinaryHeap to store PrioritizedTask and use Reverse to ensure that tasks with higher priorities are executed first. In the main function, we create two tasks, one with priority 1 and the other with priority 2. Then we add these two tasks to the event loop and execute them in the event loop.

[0120] The code for implementing execution priority in rust can be referred to as follows:

[0121]

[0122]

[0123]

[0124] In an embodiment of the present application, a method for executing asynchronous tasks is provided. By the above method, the execution priorities of multiple asynchronous operations ending in the same event can be distinguished, and a higher execution priority can be assigned to an asynchronous task with a relatively longer response time among multiple asynchronous tasks, thereby reducing processing delay.

[0125] Optionally, in the above Figure 2 Based on the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, determining the execution priority of multiple asynchronous operations includes:

[0126] The execution priorities of multiple asynchronous operations are divided based on their creation time.

[0127] In one or more embodiments, a method of dividing execution priorities is introduced. The creation time of an asynchronous operation can also be referred to as the time when the event loop mechanism obtains the asynchronous operation. After obtaining the asynchronous operation, the event loop mechanism will register these asynchronous operations to the corresponding event handler and immediately return to execute the next task. In the embodiment of the present application, the event handler is implemented by the second thread to handle asynchronous operations. The asynchronous operations may end at the same event, but the creation time or registration time of each asynchronous operation is different. The execution priority can be divided for multiple asynchronous operations based on the creation time. For example, the execution priority corresponding to the asynchronous operation with the earlier creation time is higher. For example, assuming that the creation time of asynchronous operation 1 is 1 and the creation time of asynchronous operation 2 is 2, and the creation time 1 is before the creation time 2, then the execution priority 2 can be assigned to asynchronous operation 1, and the execution priority 1 can be assigned to asynchronous operation 2. Among them, the execution priority can also be specified by the staff, which is not limited here.

[0128] In an embodiment of the present application, a method for dividing execution priorities is provided. By dividing execution priorities based on the creation time of asynchronous operations, a higher execution priority can be assigned to the asynchronous operation created earlier, thereby reducing the processing delay of the asynchronous operation.

[0129] Optionally, in the above Figure 2 On the basis of the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, the first thread takes out a task from the execution stack based on the event loop mechanism for execution, including:

[0130] The calling thread pool executes the tasks in the execution stack in parallel based on the event loop mechanism, and the threads of the thread pool include the first thread.

[0131] In one or more embodiments, a method for executing tasks in an execution stack is introduced. For single-threaded Rust, tasks can only be executed in one thread. To improve device performance, multiple threads can be configured to execute tasks in parallel. In the embodiments of the present application, multiple threads can be managed by a thread pool. The event loop mechanism can execute tasks in parallel by taking out tasks from the execution stack through multiple threads managed by the thread pool. It is understood that the multiple threads managed by the thread pool include the first thread as the main thread.

[0132] For example, the embodiment of the present application can use std::thread::ThreadPool to create a thread pool. However, this type is not disclosed in the stable version of rust, so we can use third-party libraries such as rayon or threadpool. A ThreadPool field is added to EventLoop, and a thread pool with 4 threads is created in the new method. In the run method, we use the ThreadPool::execute method to execute tasks in the thread pool. The code of the thread pool in rust can be referred to as follows:

[0133]

[0134]

[0135] In an embodiment of the present application, a method for executing tasks in an execution stack is provided. By using the above method, a thread pool is used to provide multiple threads that can execute tasks in parallel, thereby improving processing efficiency.

[0136] Optionally, in the above Figure 2 On the basis of the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, the method further includes:

[0137] When the thread pool resources are full, the event loop mechanism is dormant;

[0138] When new resources are released in the occupied thread pool, the event loop mechanism is awakened.

[0139] In one or more embodiments, a method of dormant event loop mechanism is introduced. When multiple threads of a thread pool are used to execute tasks, if a terminal device suddenly encounters a large number of concurrent tasks, the number of threads in the thread pool may not be enough to support these tasks. At this time, the thread pool may be full, and the event loop mechanism cannot continue to execute tasks from the execution stack. To avoid meaningless processor resource occupation by the event loop mechanism, the event loop mechanism can be dormant. When the tasks of threads in the full thread pool are completed and new resources are released, the event loop mechanism can be awakened again to continue to execute tasks from the execution stack using the completed threads.

[0140] In an embodiment of the present application, a method for dormant event loop mechanism is provided. By the above method, the event loop mechanism is dormant when the thread pool is fully loaded, and the event loop mechanism is awakened when resources in the thread pool are released, thereby saving processor resource usage.

[0141] Optionally, in the above Figure 2 On the basis of the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, when the execution stack is empty, extracting an asynchronous task from the task queue and adding it to the execution stack includes:

[0142] When the execution stack is empty, extract the asynchronous task from the task queue;

[0143] Add the callback function corresponding to the asynchronous task to the execution stack.

[0144] In one or more embodiments, a method for adding asynchronous tasks is described. One or more callback functions may be pre-created. For asynchronous operations that require waiting for certain operations or conditions to be met before returning a result, after a second thread has waited for the asynchronous operation's response time, the callback function may be called with the result of the asynchronous operation as a parameter of the callback function. The callback function is then added to the execution stack as an indication that the asynchronous task corresponding to the asynchronous operation can be executed by the first thread.

[0145] In an embodiment of the present application, a method for adding an asynchronous task is provided. By adding a callback function together with the asynchronous task to the execution stack, the callback function can be used to notify that the response time has ended, thereby avoiding repeated waiting and improving the efficiency of asynchronous task processing.

[0146] Optionally, in the above Figure 2On the basis of the corresponding embodiments, in another optional embodiment provided by the embodiments of the present application, the first thread takes out a task from the execution stack based on the event loop mechanism for execution, including:

[0147] The first thread reads the callback function from the execution stack based on the event loop mechanism;

[0148] Call the callback function to perform asynchronous tasks.

[0149] In one or more embodiments, a method for executing asynchronous tasks is introduced. After all synchronous tasks in the execution stack are taken out, the event loop mechanism can add asynchronous tasks and callback functions from the task queue to the execution stack. When the tasks in the execution stack of the first thread are executed, the callback function is read from the execution stack, and the callback is executed on the corresponding asynchronous task based on the callback function, that is, the data or objects related to the asynchronous task are accessed. After the callback function is executed, it may return some results or trigger other events. These results or events need to be properly processed to ensure the correct operation of the program. Among them, the reading priority of the callback function is related to the execution priority of the corresponding asynchronous task. Among them, syntactic sugar such as callback functions and async / await can be used to handle asynchronous tasks without manually managing threads or locks. This simplified programming model reduces the complexity of programming and improves development efficiency.

[0150] For example, the flowchart for executing synchronous and asynchronous operations in the embodiment of the present application can refer to Figure 3 As shown, step 301. Get the operation, which can be a synchronous operation or an asynchronous operation. Step 302. Determine whether the operation is a synchronous operation. If so, execute step 303, otherwise execute step. Step 303. Push the synchronous task corresponding to the synchronous operation into the execution stack of the main thread. Step 304. The main thread reads the synchronous tasks in the stack in sequence and executes them. Step 305. Register a callback function, wherein a callback function can be defined, and this function will be called when a specific event or condition occurs. Then, register this callback function with the system or framework so that it can be called at the appropriate time. Among them, defining the callback function and registering the callback function can be performed in advance, which is only explained here. Step 306. Add the asynchronous task corresponding to the asynchronous operation to the task queue. Specifically, use a background thread to execute the asynchronous operation. When the asynchronous operation is completed, the relevant system or framework will put the previously registered callback function into the task queue, and the asynchronous task corresponding to the asynchronous operation will also be added to the task queue. Step 307. When the task in the execution stack is completed, the event loop mechanism reads the callback function of the asynchronous task from the task queue, pushes the callback function into the execution stack for execution, and the execution stack accesses the data or objects related to the asynchronous task based on the callback function.

[0151] In another example, the processing sequence diagram for synchronous operation and asynchronous operation can refer to Figure 4 As shown, the program includes a main thread and a second thread. The execution stack of the main thread executes the synchronous tasks in the stack in sequence. If an asynchronous operation (such as a timer, a network request, an event monitoring, etc.) is encountered, the execution engine will put these asynchronous operations into the second thread for execution, and immediately return to execute the next task. The second thread executes these asynchronous operations, and when the asynchronous operation is completed, an asynchronous task is generated and added to the task queue which is a two-way queue, such as Task 1, Task 2, Task 3, etc. added in sequence in the figure. The response time representing the completion of the asynchronous operation can be the resource loading time, the time for the HTTP request to return the result, and the time for waiting for the IO event to return the result, etc. That is, when the resource loading is completed, the HTTP request return result is completed, and the IO event return result is completed, the corresponding task is added to the task queue. After the synchronous task in the execution stack is executed, the asynchronous task will be taken out from the task queue for execution, such as Figure 4 The main thread executes other tasks, which can be synchronous tasks or asynchronous tasks that are executed after all synchronous tasks are completed. The callback function corresponding to the asynchronous task before Task 1 enters the main thread's execution stack for execution. After the other task is executed, the callback function corresponding to Task 1 is retrieved and pushed onto the execution stack for execution, accessing the data or objects related to Task 1. Accordingly, after the callback function of Task 1 is executed, the callback function of Task 2 is read and executed. This will not be repeated here.

[0152] In an embodiment of the present application, a method for executing asynchronous tasks is provided. By using a callback function to execute asynchronous tasks, data and events can be passed to the function during program execution, improving program performance and responsiveness. Furthermore, the same callback function can be reused in different scenarios, thereby reducing code redundancy.

[0153] The task processing method is described above. The device for executing the method is described below. Figure 5 , Figure 5 This is a schematic diagram of an embodiment of a task processing device in an embodiment of the present application. The task processing device 50 includes:

[0154] An acquisition unit 501 is used to acquire a synchronization operation and generate a synchronization task in a Rust-based execution stack. A synchronization operation is an operation without a response time, and the execution stack is used to execute tasks that are executed immediately.

[0155] An execution unit 502 is configured to retrieve tasks from an execution stack for execution in the first thread based on an event loop mechanism, where the event loop mechanism is configured to sequentially execute tasks queued in the execution stack.

[0156] The acquisition unit 501 is further configured to acquire an asynchronous operation, where the asynchronous operation is an operation with a response time.

[0157] The execution unit 502 is further configured to create a second thread to execute an asynchronous operation, and generate an asynchronous task in the task queue after a response time; and extract an asynchronous task from the task queue and add it to the execution stack when the execution stack is empty.

[0158] In one embodiment of the present application, a task processing device is provided. By creating a second thread to perform asynchronous operations based on the Rust language execution stack, the program can simultaneously process multiple asynchronous tasks in the background, improving the system's throughput and performance. The asynchronous tasks are then continued when the execution stack is idle, ensuring the efficiency of synchronous task processing.

[0159] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the execution unit 502 is specifically configured to:

[0160] Determine execution priority for multiple asynchronous operations;

[0161] Add the asynchronous task corresponding to the asynchronous operation to the task queue. The asynchronous task includes an execution priority. The asynchronous task with a high execution priority is executed first in the execution stack.

[0162] In an embodiment of the present application, a task processing device is provided. By using the device, multiple asynchronous operations ending with the same event are prioritized, and a higher priority can be assigned to an asynchronous task with a relatively longer response time among the multiple asynchronous tasks, thereby reducing processing delay.

[0163] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the execution unit 502 is specifically configured to:

[0164] The execution priorities of multiple asynchronous operations are divided based on their creation time.

[0165] In an embodiment of the present application, a task processing device is provided. By dividing the execution priority according to the creation time of asynchronous operations, a higher execution priority can be assigned to the asynchronous operation created earlier, thereby reducing the processing delay of the asynchronous operation.

[0166] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the execution unit 502 is specifically configured to:

[0167] The calling thread pool executes the tasks in the execution stack in parallel based on the event loop mechanism, and the threads of the thread pool include the first thread.

[0168] In an embodiment of the present application, a task processing device is provided, which uses a thread pool to provide multiple threads that can execute tasks in parallel, thereby improving processing efficiency.

[0169] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the task processing device 50 further includes a sleep unit 503, and the sleep unit 503 is specifically configured to:

[0170] When the thread pool resources are full, the event loop mechanism is dormant;

[0171] When new resources are released in the occupied thread pool, the event loop mechanism is awakened.

[0172] In an embodiment of the present application, a task processing device is provided. By means of the device, an event loop mechanism is dormant when a thread pool is fully loaded, and is awakened when resources in the thread pool are released, thereby saving processor resources.

[0173] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the execution unit 502 is specifically configured to:

[0174] When the execution stack is empty, extract the asynchronous task from the task queue;

[0175] Add the callback function corresponding to the asynchronous task to the execution stack.

[0176] In an embodiment of the present application, a task processing device is provided. By adding a callback function to an execution stack together with an asynchronous task, the callback function can be used to notify that the response time has expired, thereby avoiding repeated waiting and improving the efficiency of asynchronous task processing.

[0177] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the execution unit 502 is further configured to:

[0178] The first thread reads the callback function from the execution stack based on the event loop mechanism;

[0179] Call the callback function to perform asynchronous tasks.

[0180] In an embodiment of the present application, a task processing device is provided. By using the above device, asynchronous tasks are executed using callback functions, which can pass data and events to functions during program execution, improving program performance and responsiveness. The same callback function can be reused in different scenarios, thereby reducing code redundancy.

[0181] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the task processing device 50 further includes a sleep unit 503, and the sleep unit 503 is specifically configured to:

[0182] When both the execution stack and the task queue are empty, the event loop mechanism is dormant;

[0183] When the execution stack or task queue is not empty and the event loop mechanism is dormant, wake up the event loop mechanism.

[0184] In an embodiment of the present application, a task processing device is provided. By means of the device, an event loop mechanism is dormant when no event is being processed and is awakened only when an event occurs, thereby reducing meaningless resource usage of the event loop mechanism.

[0185] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the task processing device 50 further includes a management unit 504, and the management unit 504 is specifically configured to:

[0186] Use Box pointers to manage synchronous and asynchronous tasks.

[0187] In an embodiment of the present application, a task processing device is provided. By using the above device, when a very large data structure or array is placed on the execution stack, which may cause an execution stack overflow, these large data structures can be moved to the heap using Box, thereby avoiding the execution stack overflow.

[0188] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the task processing device 50 further includes a management unit, and the management unit 504 is specifically configured to:

[0189] Arc based on Rust shares synchronous and asynchronous tasks between multiple threads.

[0190] In an embodiment of the present application, a task processing device is provided. Through the above device, Arc in Rust is used to provide a thread-safe reference counting smart pointer, making it easier and safer to share data between multiple threads.

[0191] Optionally, in the above Figure 5 On the basis of the corresponding embodiment, in another embodiment of the task processing device 50 provided in the embodiment of the present application, the management unit 504 is further configured to:

[0192] Rust-based Mutex is used to lock access to shared synchronous and asynchronous tasks.

[0193] In an embodiment of the present application, a task processing device is provided. By using a mutex lock to protect shared data from being accessed simultaneously by multiple threads, developers can safely share and access data in a multi-threaded environment.

[0194] Figure 6 : This is a schematic diagram of the structure of a computer device provided in an embodiment of the present application. The computer device 300 may have relatively large differences due to different configurations or performances, and may include one or more central processing units (CPU) 322 (for example, one or more processors) and memory 332, and one or more storage media 330 (for example, one or more mass storage devices) for storing application programs 342 or data 344. Among them, the memory 332 and the storage medium 330 can be short-term storage or persistent storage. The program stored in the storage medium 330 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations in the computer device. Furthermore, the central processing unit 322 can be configured to communicate with the storage medium 330 to execute a series of instruction operations in the storage medium 330 on the computer device 300.

[0195] The computer device 300 may also include one or more power supplies 326, one or more wired or wireless network interfaces 350, one or more input and output interfaces 358, and / or one or more operating systems 341, such as Windows Server™, Mac OS X™, Unix™, Linux™, FreeBSD™, etc.

[0196] The steps performed by the terminal device in the above embodiment can be based on the Figure 6 The computer device structure shown.

[0197] A computer-readable storage medium is also provided in an embodiment of the present application, on which a computer program is stored. When the computer program is executed by a processor, the steps of the methods described in the above embodiments are implemented.

[0198] A computer program product is also provided in an embodiment of the present application, including a computer program. When the computer program is executed by a processor, the steps of the methods described in the above embodiments are implemented.

[0199] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0200] In the several embodiments provided in this application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.

[0201] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.

[0202] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0203] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0204] As described above, the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the above embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the above embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.

Claims

1. A task processing method, characterized in that: include: Get synchronization operations and generate synchronization tasks in the Rust-based execution stack. The synchronization operations are operations without response time. The execution stack is used to execute tasks that are executed immediately. The first thread takes out tasks from the execution stack for execution based on an event loop mechanism, wherein the event loop mechanism is used to sequentially execute tasks queued in the execution stack; Obtaining an asynchronous operation, where the asynchronous operation is an operation with the response time; Creating a second thread to execute the asynchronous operation, and generating an asynchronous task in a task queue after the response time; When the execution stack is empty, the asynchronous task is extracted from the task queue and added to the execution stack.

2. The method according to claim 1, characterized in that After creating a second thread to execute the asynchronous operation, the method further includes: Determining execution priorities for the plurality of asynchronous operations; The asynchronous task corresponding to the asynchronous operation is added to the task queue, the asynchronous task includes the execution priority, and the asynchronous task with a high execution priority is executed preferentially in the execution stack.

3. The method according to claim 2, characterized in that Determining the execution priority of the plurality of asynchronous operations comprises: The execution priorities of the multiple asynchronous operations are divided based on the creation time of the multiple asynchronous operations.

4. The method according to claim 1, wherein The first thread extracts a task from the execution stack based on the event loop mechanism for execution, including: The calling thread pool executes the tasks in the execution stack in parallel based on the event loop mechanism, and the threads of the thread pool include the first thread.

5. The method according to claim 4, characterized in that The method further comprises: When the resources of the thread pool are fully occupied, hibernate the event loop mechanism; When new resources are released from the full thread pool, the event loop mechanism is awakened.

6. The method according to claim 1, characterized in that When the execution stack is empty, extracting the asynchronous task from the task queue and adding it to the execution stack includes: When the execution stack is empty, extracting the asynchronous task from the task queue; Add the callback function corresponding to the asynchronous task to the execution stack.

7. The method according to claim 6, characterized in that The first thread takes out a task from the execution stack based on the event loop mechanism and executes it, which includes: Reading the callback function from the execution stack based on the event loop mechanism in the first thread; The callback function is called to execute the asynchronous task.

8. The method according to claim 1, characterized in that The method further comprises: When the execution stack and the task queue are both empty, hibernating the event loop mechanism; When the execution stack or the task queue is not empty and the event loop mechanism is dormant, the event loop mechanism is awakened.

9. The method according to claim 1, characterized in that The method further comprises: Box pointers are used to manage the synchronous tasks and the asynchronous tasks.

10. The method according to claim 1, characterized in that The method further comprises: Rust-based Arc shares the synchronous task and the asynchronous task among multiple threads.

11. The method according to claim 10, characterized in that The method further comprises: The shared synchronous task and the shared asynchronous task are access-locked based on rust's Mutex.

12. A task processing device, characterized in that: include: An acquisition unit, configured to acquire a synchronization operation and generate a synchronization task in a Rust-based execution stack, wherein the synchronization operation is an operation without response time, and the execution stack is used to execute tasks that are executed immediately; An execution unit, configured to fetch tasks from the execution stack for execution in a first thread based on an event loop mechanism, wherein the event loop mechanism is configured to sequentially execute tasks queued in the execution stack; The acquisition unit is further configured to acquire an asynchronous operation, where the asynchronous operation is an operation having the response time; The execution unit is further configured to create a second thread to execute the asynchronous operation and generate an asynchronous task in a task queue after the response time; and when the execution stack is empty, extract the asynchronous task from the task queue and add it to the execution stack.

13. A computer device, characterized in that: include: memories, transceivers, processors, and bus systems; Wherein, the memory is used to store programs; The processor is configured to execute the program in the memory, including executing the method according to any one of claims 1 to 11; The bus system is used to connect the memory and the processor so that the memory and the processor can communicate with each other.

14. A computer-readable storage medium comprising instructions, which, when executed on a computer, causes the computer to perform the method according to any one of claims 1 to 11.

15. A computer program product, characterized in that When the computer program product is executed on a computer, the computer performs the method according to any one of claims 1 to 11.