A file processing method, device, electronic device and computer readable medium

By generating polling objects, each process can edit the file descriptor status, realize multiplexing of file operations, solve the problem of inefficiency when multiple processes frequently read and write, and realize efficient reading and writing of files between processes.

CN114327404BActive Publication Date: 2024-12-24CCB FINTECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111626169.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-28
Publication Date
2024-12-24
Estimated Expiration
2041-12-28

AI Technical Summary

Technical Problem

When multiple processes frequently read and write operations on the same file, the read and write processing efficiency of the file is inefficient.

Method used

By receiving file processing requests, obtaining file descriptors, calling constructors to generate polling instances, and registering file descriptors based on polling objects, multiplexing of files is realized, allowing each process to open files at the same time for sequential reading and writing.

Benefits of technology

It improves file read and write processing efficiency, reduces changes to business code, and realizes efficient multiplexing of file operations between processes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114327404B_ABST
    Figure CN114327404B_ABST
Patent Text Reader

Abstract

The present application discloses a file processing method, device, electronic device and computer-readable medium, and relates to the field of automatic programming technology. A specific implementation method includes: receiving a file processing request and obtaining a corresponding file descriptor; calling a constructor to generate a polling instance based on the file descriptor; calling the polling instance to register the file descriptor, and then generating a polling object; and executing the corresponding file processing based on the polling object. By generating a polling object, each process can edit the state of the polling object at the same time, so that the multiplexing of file operations can be realized based on the state of the file descriptor, so that one process can monitor multiple file descriptors, and each process can open the file at the same time, read and write sequentially at the same time, and reduce the modification of the business code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of automatic programming technology, and in particular to a file processing method, device, electronic device and computer-readable medium. Background Art

[0002] On the server side of software development, there will be frequent alternation of reading and writing files between processes. In real software development, the file reading and writing operations of the Go language are blocking. When multiple processes frequently read and write the same file, one process opens a file for writing, and other processes can only read and write the file until the process closes the file, resulting in low efficiency in file reading and writing.

[0003] In the process of implementing this application, the inventors found that there are at least the following problems in the prior art:

[0004] When multiple processes frequently perform read and write operations on the same file, the read and write processing efficiency of the file is low. Summary of the invention

[0005] In view of this, embodiments of the present application provide a file processing method, device, electronic device and computer-readable medium, which can solve the problem of low efficiency in reading and writing files when multiple processes frequently perform read and write operations on the same file.

[0006] To achieve the above object, according to one aspect of an embodiment of the present application, a file processing method is provided, comprising:

[0007] Receive file processing request and obtain the corresponding file descriptor;

[0008] Call the constructor to generate a polling instance based on the file descriptor;

[0009] Call the polling instance to register the file descriptor and generate the polling object;

[0010] The processing of the corresponding file is performed based on the polling object.

[0011] Optionally, generate a polling instance based on the file descriptor, including:

[0012] Call the constructor to create a listening object based on the file descriptor and encapsulate the listening object;

[0013] Start a separate coroutine to generate an infinite loop based on the encapsulated listener object;

[0014] Execute an infinite loop and then call the monitoring function to obtain the status of the encapsulated monitoring object;

[0015] Generates a polling instance based on the state of the encapsulated listening object.

[0016] Optionally, call the polling instance to register the file descriptor, and then generate a polling object, including:

[0017] Call the listening event registration function to register the file descriptor to the polling instance, thereby generating a polling object.

[0018] Optionally, call the event registration function of the listener to register the file descriptor to the polling instance, and then generate a polling object, including:

[0019] Call the listening event registration function to encapsulate the polling instance and file descriptor, and set the encapsulated file descriptor to non-blocking, thereby obtaining the polling object.

[0020] Optionally, executing corresponding file processing based on the polling object includes:

[0021] Determine the execution method type corresponding to the polling object;

[0022] Execute the corresponding file processing according to the execution method type.

[0023] Optionally, the method further comprises:

[0024] Get the status returned by the processing of the corresponding file, and then adjust the status of the corresponding polling object according to the returned status.

[0025] Optionally, the execution method type includes:

[0026] One or more of file read, file write, file read wait, file write wait and file close.

[0027] In addition, the present application also provides a file processing device, including:

[0028] A receiving unit is configured to receive a file processing request and obtain a corresponding file descriptor;

[0029] An instance generation unit is configured to call a constructor to generate a polling instance based on a file descriptor;

[0030] The object generation unit is configured to call the polling instance to register the file descriptor, thereby generating a polling object;

[0031] The processing unit is configured to execute processing of the corresponding file based on the polling object.

[0032] Optionally, the instance generation unit is further configured to:

[0033] Call the constructor to create a listening object based on the file descriptor and encapsulate the listening object;

[0034] Start a separate coroutine to generate an infinite loop based on the encapsulated listener object;

[0035] Execute an infinite loop and then call the monitoring function to obtain the status of the encapsulated monitoring object;

[0036] Generates a polling instance based on the state of the encapsulated listening object.

[0037] Optionally, the object generation unit is further configured to:

[0038] Call the listening event registration function to register the file descriptor to the polling instance, thereby generating a polling object.

[0039] Optionally, the object generation unit is further configured to:

[0040] Call the listening event registration function to encapsulate the polling instance and file descriptor, and set the encapsulated file descriptor to non-blocking, thereby obtaining the polling object.

[0041] Optionally, the processing unit is further configured to:

[0042] Determine the execution method type corresponding to the polling object;

[0043] Execute the corresponding file processing according to the execution method type.

[0044] Optionally, the device further includes a state adjustment unit configured to:

[0045] Get the status returned by the processing of the corresponding file, and then adjust the status of the corresponding polling object according to the returned status.

[0046] Optionally, the execution method type includes:

[0047] One or more of file read, file write, file read wait, file write wait and file close.

[0048] In addition, the present application also provides a file processing electronic device, including: one or more processors; a storage device for storing one or more programs, when the one or more programs are executed by one or more processors, the one or more processors implement the file processing method as described above.

[0049] In addition, the present application also provides a computer-readable medium on which a computer program is stored, and when the program is executed by a processor, the file processing method as described above is implemented.

[0050] To achieve the above objective, according to another aspect of the embodiments of the present application, a computer program product is provided.

[0051] A computer program product according to an embodiment of the present application includes a computer program, and when the program is executed by a processor, the file processing method provided by the embodiment of the present application is implemented.

[0052] An embodiment of the above invention has the following advantages or beneficial effects: the application receives a file processing request to obtain a corresponding file descriptor; calls a constructor to generate a polling instance based on the file descriptor; calls the polling instance to register the file descriptor, thereby generating a polling object; and performs corresponding file processing based on the polling object. By generating a polling object, each process can edit the state of the polling object at the same time, thereby multiplexing file operations based on the state of the file descriptor, enabling a process to monitor multiple file descriptors, enabling each process to open a file at the same time, and performing sequential simultaneous reading and writing, and reducing changes to the business code.

[0053] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with specific implementation examples. BRIEF DESCRIPTION OF THE DRAWINGS

[0054] The accompanying drawings are used to better understand the present application and do not constitute an improper limitation on the present application.

[0055] Figure 1 is a schematic diagram of the main process of the file processing method according to the first embodiment of the present application;

[0056] Figure 2 is a schematic diagram of the main process of the file processing method according to the second embodiment of the present application;

[0057] Figure 3 is a schematic diagram of an application scenario of a file processing method according to a third embodiment of the present application;

[0058] Figure 4 is a schematic diagram of main units of a file processing device according to an embodiment of the present application;

[0059] Figure 5 is an exemplary system architecture diagram to which the embodiments of the present application can be applied;

[0060] Figure 6 It is a structural diagram of a computer system of a terminal device or server suitable for implementing an embodiment of the present application. DETAILED DESCRIPTION

[0061] The following is a description of exemplary embodiments of the present application in conjunction with the accompanying drawings, including various details of the embodiments of the present application to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present application. Similarly, for the sake of clarity and conciseness, the description of well-known functions and structures is omitted in the following description. The acquisition, storage, use, and processing of data in the technical solution of the present application are in compliance with the relevant provisions of national laws and regulations.

[0062] Figure 1 is a schematic diagram of the main process of the file processing method according to the first embodiment of the present application, such as Figure 1 As shown, the file processing method includes:

[0063] Step S101, receiving a file processing request and obtaining a corresponding file descriptor.

[0064] In this embodiment, epoll in the embodiment of the present application is the same as EPOLL. Epoll is an improved poll for processing large batches of file descriptors in the Linux kernel. It is an enhanced version of the multiplexed IO interface select / poll under Linux. It can significantly improve the system CPU utilization of the program when only a small number of concurrent connections are active. When obtaining an event, epoll does not need to traverse the entire set of descriptors being listened to, but only needs to traverse the set of file descriptors that are asynchronously awakened by the kernel IO event and added to the Ready queue. The execution subject of the file processing method (for example, it can be a server) can receive a file processing request through a wired connection or a wireless connection. Specifically, the file processing request can be a request to read, write, read wait, and write wait for a file. The embodiment of the present application does not limit the specific content of the request for file processing. After receiving the file processing request, the execution subject can obtain the file path (path) and operation mode in the file processing request, where the operation mode represents the file opening mode, such as read, write, etc. Then, the execution subject can open the corresponding file based on the file path and operation mode corresponding to the obtained file processing request. After opening the corresponding file, the execution subject can receive a returned file descriptor. The kernel uses file descriptors (fd) to access files. File descriptors are non-negative integers. When opening an existing file or creating a new file, the kernel returns a file descriptor. Reading and writing files also require the use of file descriptors to specify the file to be read and written. The file descriptor is a handle represented by an unsigned integer, which the process uses to identify the open file. The file descriptor is associated with a file object that includes relevant information (such as the file's opening mode, the file's location type, the file's initial type, etc.), which is called the file context.

[0065] Step S102: calling a constructor to generate a polling instance based on a file descriptor.

[0066] Constructor: When called with the new keyword, it can implement a certain type of function, which is called a constructor. The core of the constructor is the keyword new. For example, new f1, / / f1{} and new f1() have the same effect. They are run by new and are called constructors, which are used to create an instance corresponding to a class. Specifically, the process of calling a constructor to create a polling instance can be: calling the constructor to automatically create an empty object; setting the file descriptor's attributes such as readable, writable, read wait, write wait, and closed and associating it with the this pointer inside the constructor, and then pointing the this pointer inside the function to the created object (that is, the role of the constructor, constructing a new object, setting the object's attributes and methods); automatically returning the created object or instantiated object (that is, the polling instance) after the function is executed. For example, the polling instance can be an instance of the Poller class.

[0067] In this embodiment, first, after receiving the file processing request, the execution subject will define it for later use. The pseudo code for specifically defining the Poller type (in this embodiment of the application, the Poller type is the polling type) is as follows:

[0068] / / Define the Poller type for later use

[0069] type Poller struct {

[0070] / / ...encapsulate epoll-related information (i.e. encapsulate information related to the callback function on the listening file descriptor)

[0071] }

[0072] Among them, "type Poller struct{" means using the structure to define a type named Poller.

[0073] In addition, the execution subject can also define the Pollable type for later use. The Pollable type is the type of the polling object. The specific pseudo code is as follows:

[0074] / / Define the Pollable type for later use

[0075] type Pollable struct {

[0076] / / Encapsulate Poller object and file descriptor df

[0077] }

[0078] Among them, "type Pollable struct{" means using a structure to define a type called Pollable.

[0079] Among them, epoll uses the mechanism of event callback function to detect whether the file descriptor is ready, and adds the ready file descriptor to the ready queue. The listening function, such as epoll_wait, can directly access the ready queue to determine the ready file descriptor. Specifically, epoll will notify which binary stream has what kind of I / O event. Epoll is event-driven (each event is associated with a file descriptor fd), and the operations on these binary streams are meaningful at this time. Epoll-related information includes: file descriptor. The file descriptor is an integer. For example, the descriptor of the demo.txt file can be expressed as: $demo.txt fd=3. The execution subject can encapsulate the epoll-related information associated with the file descriptor, and then define the Poller type.

[0080] After defining the Poller type, the execution body can call the New function to generate a Poller class instance based on the Poller type defined by the epoll-related information associated with the file descriptor.

[0081] The specific Go language pseudo code for generating an instance of the Poller class is as follows: p, err: = New(), which means calling the New function to obtain an instance p of the Poller type, where err is the error message. The New function generates an object by default. The this inside the New function points to the object generated by New by default. The object generated by the New function is the default return value of the function.

[0082] Specifically, the Go language pseudo code for generating a Poller class instance is as follows:

[0083] / / Provide a New function to create an instance of the Poller type

[0084] func New(){

[0085] / / Make a system call syscall.SYS_EPOLL_CREATE1, syscall.EPOLL_CLOEXEC to create an epoll object and encapsulate it

[0086] / / Start a separate coroutine and make this epoll object into an infinite for loop to handle system event notifications

[0087] go for {

[0088] / / Get the status of the monitored file descriptor through the system call syscall.SYS_EPOLL_WAIT

[0089] / / If the status of the monitored file descriptor is wrong or fd is closed, jump out of this for loop

[0090] / / If the event notification has syscall.EPOLLOUT, wake up the processing function to perform file operations

[0091] / / Otherwise continue this for loop

[0092] / / When jumping out of this loop, execute the system call syscall.Close to end the file descriptor

[0093] }

[0094] / / The function returns the previously encapsulated epoll object Poller

[0095] }

[0096] Specifically, the previously encapsulated epoll object Poller returned by the function is the generated polling instance (eg, Poller class instance).

[0097] Among them, "func New(){" in the go language pseudocode for generating the Poller class instance means defining a public function New; "syscall.SYS_EPOLL_CREATE1" means the system call to create an epoll instance; "syscall.EPOLL_CLOEXEC" means the parameters used to create the epoll instance, and the epoll handle will be automatically closed after exec is executed; "syscall.SYS_EPOLL_WAIT" means the status flag of the epoll instance, which is used to identify the status of the file descriptor; "syscall.EPOLLOUT" means the status flag of the epoll instance, which represents the output event, and the program used to handle the event is removed at this time; "syscall.Close" means to close the file descriptor.

[0098] Step S103, calling the polling instance to register the file descriptor, and then generating a polling object.

[0099] Specifically, the polling instance, for example, can be an instance of the Poller class. Register a file descriptor df, add it to the listening event loop, and return a polling object (for file read / write / close / wait operations). The Go language pseudo code for generating the polling object is as follows:

[0100] / / Poller object method Register: registers a file descriptor df, adds it to the epoll event loop, and returns a Pollable object (for file read / write / close / wait operations)

[0101] func(*Poller)Register(fd){

[0102] / / Encapsulate a Pollable object, including the Poller object and file descriptor fd

[0103] / / Set the file descriptor fd to non-blocking (using the system call syscall.SetNonblock)

[0104] / / Register the file descriptor fd with the Poller object created by the previous New() function, as follows:

[0105] / / Register the file descriptor fd to epoll through the system calls syscall.SYS_EPOLL_CTL and syscall.EPOLL_CTL_ADD

[0106] / / This function returns the previously encapsulated Pollable object (that is, returns the polling object)

[0107] }

[0108] Among them, "Register" in the go language pseudocode that generates the polling object (also known as the Pollable object) means registration; "func(*Poller)Register(fd){" ​​means defining an execution method Register of the Poller type, with fd (file descriptor) as a parameter; "syscall.SetNonblock" means setting the file descriptor fd to a non-blocking state; "syscall.SYS_EPOLL_CTL" means using this system call to change the epoll instance. EPOLL_CTL adds, modifies, or deletes events of interest to the epoll object, and returns 0 for success, otherwise -1. At this time, the error type needs to be determined based on the errno error code. The event returned by the EPOLL_WAIT method must be added to epoll through EPOLL_CTL; "syscall.EPOLL_CTL_ADD" means adding the file descriptor to the epoll instance for management.

[0109] Step S104: executing corresponding file processing based on the polling object.

[0110] Specifically, the polling object can support various methods Read, Write, WaitRead, WaitWrite, and Close for file operations.

[0111] Specifically, the processing of executing the corresponding file based on the polling object includes: determining the execution method type corresponding to the polling object.

[0112] Specifically, the execution method types include: one or more of: file read, file write, file read wait, file write wait and file close.

[0113] Execute the corresponding file processing according to the execution method type.

[0114] In response to the execution method type being file read, the execution subject may call an open file read function, such as "func(p*Pollable)Read(buff){" to perform a file read operation.

[0115] For example, the Go language pseudo code for the execution subject to perform a file read operation is as follows:

[0116] / / Open file reading function

[0117] func(p*Pollable)Read(buff){

[0118] for{ / / Loop to read the file content

[0119] / / Use the system call syscall.Read to read the file to buff

[0120] / / If the status returned by reading the file previously is not syscall.EAGAIN, then exit this function

[0121] / / Execute the system call epoll syscall.EPOLL_CTL_MOD, syscall.EPOLLONESHOT|syscall.EPOLLIN, and set this Pollable object to the read wait state

[0122] / / Then jump out of this function

[0123] }

[0124] }

[0125] Among them, "func(p*Pollable)Read(buff){" in the go language pseudocode that executes the file read operation means defining an execution method Read of the Pollable type, with buff as a parameter; "syscall.EPOLL_CTL_MOD" means using this system call to change the epoll instance; "syscall.EPOLLONESHOT|syscall.EPOLLIN" means the parameter of Epoll, which is used to set the file descriptor in epoll to the read wait state.

[0126] In response to the execution method type being file writing, the execution subject may call an open file writing function, such as "func(p*Pollable)Write(buff){", to perform the file writing operation.

[0127] For example, the Go language pseudo code for the execution subject to perform a file write operation is as follows:

[0128] / / Open file writing function

[0129] func(p*Pollable)Write(buff){

[0130] for{ / / Loop write file

[0131] / / Use the system call syscall.Write to write the buff content to the file corresponding to the file descriptor in the Pollable object

[0132] / / If the status returned by writing the file previously is not syscall.EAGAIN, then exit this function

[0133] / / Execute the system call epoll syscall.EPOLL_CTL_MOD, syscall.EPOLLONESHOT|syscall.EPOLLOUT, and set this Pollable object to the write wait state

[0134] / / Then jump out of this function

[0135] }

[0136] }

[0137] Among them, "func(p*Pollable)Write(buff){" in the go language pseudocode that executes the file write operation means defining an execution method Write of the Pollable type, with buff as a parameter; "syscall.EPOLL_CTL_MOD" means using this system call to change the epoll instance; "syscall.EPOLLONESHOT|syscall.EPOLLOUT" means the parameter of epoll, which is used to set the file descriptor in epoll to the write wait state.

[0138] In response to the execution method type being read-wait, the execution subject may call an open function, such as "func(p*Pollable)WaitRead(){", to set the Pollable object to the read-wait state.

[0139] For example, the Go language pseudo code for the execution subject to perform the file read wait operation is as follows:

[0140] / / Open function, set the Pollable object to read wait state

[0141] func(p*Pollable)WaitRead(){

[0142] / / Execute the system call epoll syscall.EPOLL_CTL_MOD, syscall.EPOLLONESHOT|syscall.EPOLLIN, and set this Pollable object to the read wait state

[0143] / / Then jump out of this function

[0144] }

[0145] Among them, "func(p*Pollable)WaitRead(){" in the go language pseudocode that executes the file read wait operation means defining an execution method WaitRead of the Pollable type, with no parameters; "syscall.EPOLL_CTL_MOD" means using this system call to change the epoll instance; "syscall.EPOLLONESHOT|syscall.EPOLLIN" means the parameter of epoll, which is used to set the file descriptor in epoll to the read wait state.

[0146] In response to the execution method type being write-wait, the execution subject may call an open function, for example, "func(p*Pollable)WaitWrite(){" to set the Pollable object to a write-wait state.

[0147] For example, the Go language pseudo code for the execution subject to execute the file write wait operation is as follows:

[0148] / / Open function, set the Pollable object to write wait state

[0149] func(p*Pollable)WaitWrite(){

[0150] / / Execute the system call epoll syscall.EPOLL_CTL_MOD, syscall.EPOLLONESHOT|syscall.EPOLLOUT, and set this Pollable object to the write wait state

[0151] / / Then jump out of this function

[0152] }

[0153] Among them, "func(p*Pollable)WaitWrite(){" in the go language pseudocode that executes the file write wait operation means defining an execution method WaitWrite of the Pollable type, with no parameters; "syscall.EPOLL_CTL_MOD" means using this system call to change the epoll instance; "syscall.EPOLLONESHOT|syscall.EPOLLOUT" means the parameter of epoll, which is used to set the file descriptor in epoll to the write wait state.

[0154] In response to the execution method type being file closing, the execution subject may call an externally open function to close the Pollable object and the file descriptor fd it encapsulates.

[0155] For example, the Go language pseudo code for the execution subject to perform the file closing operation is as follows:

[0156] / / Open function, Pollable object and its encapsulated file descriptor fd are closed

[0157] func(p*Pollable)Close(){

[0158] / / Execute the system call syscall.EPOLL_CTL_DEL to delete the file descriptor fd from epoll

[0159] }

[0160] Among them, "func(p*Pollable)Close(){" in the go language pseudocode that executes the file closing operation means defining an execution method Close of the Pollable type with no parameters; "syscall.EPOLL_CTL_DEL" means using this system call to delete the file descriptor of the epoll instance.

[0161] Specifically, the file processing method further includes:

[0162] Get the status returned by the processing of the corresponding file, and then adjust the status of the corresponding polling object according to the returned status.

[0163] The execution subject can obtain the status of the file descriptor returned by the processing of the file, such as whether the file descriptor is ready, for example, whether it is readable or writable. The execution subject can adjust the status of the corresponding polling object (such as the Pollable object) according to the status of the returned file descriptor. For example, when the file descriptor is not readable, the status of the corresponding polling object is adjusted to read waiting, and when the file descriptor is not writable, the status of the corresponding polling object is adjusted to write waiting. For epoll, the file descriptor passed to the user is ready, and there is no need to traverse it, thereby saving the traversal time of judging whether the file passed to the user is ready, and realizing IO multiplexing. IO multiplexing: It is a synchronous IO model that enables a process to monitor multiple file handles. The file handle is the file descriptor. Once a file handle is ready, it can notify the application to perform the corresponding read and write operations. If there is no file handle ready, the application will be blocked and the CPU will be handed over. Multiplexing refers to network connection, and multiplexing refers to the same thread.

[0164] This embodiment receives a file processing request to obtain a corresponding file descriptor; calls a constructor to generate a polling instance based on the file descriptor; calls the polling instance to register the file descriptor, thereby generating a polling object; and performs corresponding file processing based on the polling object. By generating a polling object, each process can edit the state of the polling object at the same time, thereby multiplexing file operations based on the state of the file descriptor, enabling a process to monitor multiple file descriptors, enabling each process to open a file at the same time, and performing sequential simultaneous reading and writing, and reducing changes to the business code.

[0165] Figure 2 is a schematic diagram of the main flow of the file processing method according to the second embodiment of the present application, such as Figure 2 As shown, the file processing method includes:

[0166] Step S201, receiving a file processing request and obtaining a corresponding file descriptor.

[0167] Step S202: calling a constructor to create a monitoring object based on the file descriptor and encapsulate the monitoring object.

[0168] The execution subject can associate the file descriptor with the monitoring-related information, and then call the constructor based on the monitoring-related information after associating the file descriptor to create a monitoring object, and encapsulate the created monitoring object.

[0169] Step S203, starting an independent coroutine to generate an infinite loop based on the encapsulated monitoring object.

[0170] The execution subject can start an independent coroutine separately and make the encapsulated listening object into an infinite loop to handle system event notifications.

[0171] Step S204, executing an infinite loop, and then calling the monitoring function to obtain the state of the encapsulated monitoring object.

[0172] Execute an infinite loop (such as an infinite for loop), obtain the status of the monitored file descriptor through the system call syscall.SYS__WAIT, and then update the status of the encapsulated monitoring object (i.e., epoll object) according to the status of the file descriptor, such as whether it is a read wait state or a write wait state.

[0173] Step S205: Generate a polling instance based on the state of the encapsulated monitoring object.

[0174] The execution subject can call the constructor to generate a corresponding polling instance according to the specific state of the encapsulated monitoring object, such as a read wait state or a write wait state.

[0175] Step S206, calling the polling instance to register the file descriptor, and then generating a polling object.

[0176] Specifically, the polling instance is called to register the file descriptor, and then the polling object is generated, including:

[0177] Call the epoll event registration function to register the file descriptor to the polling instance (ie, Pollable instance), and then generate a polling object (ie, Pollable object).

[0178] Specifically, "registering to" means "encapsulation", that is, the execution subject can call the event registration function of epoll to encapsulate the file descriptor into the polling instance (ie, Pollable instance) to generate a polling object (ie, Pollable object).

[0179] Specifically, the event registration function of epoll is called to register the file descriptor to the polling instance, thereby generating a polling object, including:

[0180] Call the epoll event registration function to encapsulate the polling instance and file descriptor, and set the encapsulated file descriptor to non-blocking to obtain the polling object.

[0181] Step S207: executing corresponding file processing based on the polling object.

[0182] The embodiments of the present application can realize multiplexing of file operations, enable each process to open the file at the same time, perform sequential simultaneous reading and writing, and reduce changes to business codes.

[0183] Figure 3 Schematic diagram of an application scenario of the file processing method according to the third embodiment of the present application. The file processing method of the embodiment of the present application can be applied to a scenario where multiple processes read and write files at the same time. Figure 3 As shown, the server 302 receives the file processing request 301 and obtains the corresponding file descriptor 303. The server 302 calls the constructor 304 to generate a polling instance 305 based on the file descriptor 303. The server 302 calls the polling instance 305 to register the file descriptor 303, and then generates a polling object 306. The server 302 performs the processing of the file 307 corresponding to the file processing request 301 based on the polling object 306.

[0184] For example, the pseudo code for implementing file processing in the embodiment of the present application is as follows:

[0185] / / Pseudo code format for multiplexing

[0186] file,err:=os.Open("tmp.txt"); / / go language opens a file

[0187] p,err:=New(); / / Call the new function to get an instance of Poller type p

[0188] r,err:=p.Register(file.pfd); / / Register file descriptor file.pfd, obtain Pollable object r

[0189] n,err:=r.Read(buf); / / Call the Read method of Pollable object r to read the file content

[0190] …

[0191] Among them, "file,err:=p.Register(file.pfd)" means opening a file, file is the file handle, err is the error message, and "tmp.txt" is the file name.

[0192] “p, err:=New()” means calling the new function to obtain an instance p of the Poller type; err is the error message.

[0193] “r, err:=p.Register(file.pfd)” means registering the file descriptor file.pfd and obtaining the Pollable object r; err is the error message; file.pfd is the pfd attribute (file descriptor) of the file object obtained previously.

[0194] “n,err:=r.Read(buf)” means reading file information, n is the length of the read content, err is the error message, and buf is the place where the read content is written.

[0195] The embodiment of the present application obtains an instance object of the Poller type by calling the new function; registers the file descriptor file.pfd with the Poller object to obtain a Pollable object; and then uses the Pollable object to perform the same file read and write operations as before. Multiplexing of files is achieved, so that each process can open the file at the same time, read and write sequentially at the same time, and reduce the changes to the business code.

[0196] Figure 4 Schematic diagram of the main units of the file processing device according to an embodiment of the present application. Figure 4 As shown, the file processing device includes a receiving unit 401, an instance generating unit 402, an object generating unit 403 and a processing unit 404.

[0197] The receiving unit 401 is configured to receive a file processing request and obtain a corresponding file descriptor.

[0198] The instance generation unit 402 is configured to call the constructor to generate a polling instance based on the file descriptor.

[0199] The object generation unit 403 is configured to call the polling instance registration file descriptor, and then generate a polling object.

[0200] The processing unit 404 is configured to execute corresponding file processing based on the polling object.

[0201] In some embodiments, the instance generation unit is further configured to: call the constructor to create a listening object based on the file descriptor and encapsulate the listening object; start an independent coroutine to generate an infinite loop based on the encapsulated listening object; execute the infinite loop, and then call the listening function to obtain the status of the encapsulated listening object; generate a polling instance based on the status of the encapsulated listening object.

[0202] In some embodiments, the object generation unit 403 is further configured to: call the monitored event registration function to register the file descriptor to the polling instance, thereby generating a polling object.

[0203] In some embodiments, the object generation unit 403 is further configured to: call the monitored event registration function to encapsulate the polling instance and the file descriptor, and set the encapsulated file descriptor to non-blocking, thereby obtaining the polling object.

[0204] In some embodiments, the processing unit 404 is further configured to: determine the execution method type corresponding to the polling object; and execute the processing of the corresponding file according to the execution method type.

[0205] In some embodiments, the device further comprises Figure 4 The state adjustment unit not shown in the figure is configured to: obtain the status returned by the processing of the corresponding file, and then adjust the state of the corresponding polling object according to the returned status.

[0206] In some embodiments, the execution method type includes: one or more of: file read, file write, file read wait, file write wait and file close.

[0207] It should be noted that the file processing method and the file processing device in the present application have a corresponding relationship in terms of specific implementation contents, so the repeated contents will not be described again.

[0208] Figure 5 An exemplary system architecture 500 is shown to which the file processing method or file processing device according to the embodiment of the present application can be applied.

[0209] like Figure 5 As shown, system architecture 500 may include terminal devices 501, 502, 503, a network 504 and a server 505. Network 504 is used to provide a medium for communication links between terminal devices 501, 502, 503 and server 505. Network 504 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.

[0210] Users can use terminal devices 501, 502, 503 to interact with server 505 through network 504 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 501, 502, 503, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only as examples).

[0211] The terminal devices 501 , 502 , and 503 may be various electronic devices having a file processing screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, and the like.

[0212] Server 505 may be a server that provides various services, such as a background management server that provides support for file processing requests submitted by users using terminal devices 501, 502, and 503 (only as an example). The background management server may receive a file processing request and obtain a corresponding file descriptor; call a constructor to generate a polling instance based on the file descriptor; call the polling instance to register the file descriptor, thereby generating a polling object; and perform corresponding file processing based on the polling object. By generating a polling object, each process may simultaneously edit the state of the polling object, realize multiplexing of file operations, realize opening files simultaneously by each process, read and write sequentially at the same time, and reduce changes to the business code.

[0213] It should be noted that the file processing method provided in the embodiment of the present application is generally executed by the server 505 , and accordingly, the file processing device is generally arranged in the server 505 .

[0214] It should be understood that Figure 5 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to the implementation requirements.

[0215] Reference below Figure 6 , which shows a schematic diagram of the structure of a computer system 600 of a terminal device suitable for implementing an embodiment of the present application. Figure 6 The terminal device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.

[0216] like Figure 6As shown, the computer system 600 includes a central processing unit (CPU) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage part 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the computer system 600 are also stored. The CPU 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.

[0217] The following components are connected to the I / O interface 605: an input section 606 including a keyboard, a mouse, etc.; an output section 607 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, a modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the I / O interface 605 as needed. A removable medium 611, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 610 as needed, so that a computer program read therefrom is installed into the storage section 608 as needed.

[0218] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 609, and / or installed from the removable medium 611. When the computer program is executed by the central processing unit (CPU) 601, the above-mentioned functions defined in the system of the present application are executed.

[0219] It should be noted that the computer-readable medium shown in the present application may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. Computer-readable storage media may include, for example, but are not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present application, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0220] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the above-mentioned module, program segment or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0221] The units involved in the embodiments described in the present application may be implemented by software or hardware. The units described may also be set in a processor, for example, it may be described as: a processor includes a receiving unit, an instance generation unit, an object generation unit, and a processing unit. The names of these units do not constitute limitations on the units themselves in some cases.

[0222] As another aspect, the present application also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs. When the above one or more programs are executed by a device, the device receives a file processing request and obtains a corresponding file descriptor; calls a constructor to generate a polling instance based on the file descriptor; calls the polling instance to register the file descriptor, and then generates a polling object; and performs corresponding file processing based on the polling object. By generating a polling object, each process can edit the status of the polling object at the same time, realize multiplexing of file operations, realize each process to open the file at the same time, read and write sequentially at the same time, and reduce changes to the business code.

[0223] The computer program product of the present application includes a computer program, and when the computer program is executed by a processor, the file processing method in the embodiment of the present application is implemented.

[0224] According to the technical solution of the embodiment of the present application, by generating a polling object, each process can edit the status of the polling object at the same time, so that multiplexing of file operations can be achieved based on the status of the file descriptor, so that one process can monitor multiple file descriptors, and each process can open the file at the same time, perform sequential simultaneous reading and writing, and reduce changes to the business code.

[0225] The above specific implementations do not constitute a limitation on the protection scope of this application. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of this application should be included in the protection scope of this application.

Claims

1. A file processing method, characterized in that: include: Receive file processing request and obtain the corresponding file descriptor; Calling a constructor to generate a polling instance based on the file descriptor; Generating a polling instance based on the file descriptor includes: calling a constructor to create a monitoring object based on the file descriptor and encapsulating the monitoring object; starting an independent coroutine to generate an infinite loop based on the encapsulated monitoring object; executing the infinite loop, and then calling a monitoring function to obtain the state of the encapsulated monitoring object; generating a polling instance based on the state of the encapsulated monitoring object; Calling the polling instance to register the file descriptor, thereby generating a polling object; calling the polling instance to register the file descriptor, thereby generating a polling object, includes: calling the monitored event registration function to register the file descriptor to the polling instance, thereby generating a polling object; calling the monitored event registration function to register the file descriptor to the polling instance, thereby generating a polling object, includes: calling the monitored event registration function to encapsulate the polling instance and the file descriptor, and setting the encapsulated file descriptor to non-blocking, thereby obtaining a polling object; The processing of the corresponding file is performed based on the polling object.

2. The method according to claim 1, characterized in that The processing of executing the corresponding file based on the polling object includes: Determine the execution method type corresponding to the polling object; The corresponding file processing is performed according to the execution method type.

3. The method according to claim 2, characterized in that The method further comprises: The status returned by the processing of the corresponding file is obtained, and then the status of the corresponding polling object is adjusted according to the returned status.

4. The method according to claim 2, characterized in that: The execution method types include: One or more of file read, file write, file read wait, file write wait and file close.

5. A file processing device, characterized in that: include: A receiving unit is configured to receive a file processing request and obtain a corresponding file descriptor; An instance generation unit, configured to call a constructor to generate a polling instance based on the file descriptor; The instance generation unit is further configured to: call a constructor to create a monitoring object based on the file descriptor and encapsulate the monitoring object; Start a separate coroutine to generate an infinite loop based on the encapsulated listener object; Execute the infinite loop, and then call the monitoring function to obtain the state of the encapsulated monitoring object; Generate a polling instance based on the state of the encapsulated monitoring object; an object generation unit, configured to call the polling instance to register the file descriptor, and then generate a polling object; The object generation unit is further configured to: call the monitored event registration function to register the file descriptor to the polling instance, thereby generating a polling object; the object generation unit is further configured to: call the monitored event registration function to encapsulate the polling instance and the file descriptor, and set the encapsulated file descriptor to non-blocking, thereby obtaining a polling object; The processing unit is configured to execute corresponding file processing based on the polling object.

6. The device according to claim 5, characterized in that The processing unit is further configured to: Determine the execution method type corresponding to the polling object; The corresponding file processing is performed according to the execution method type.

7. The device according to claim 6, characterized in that The device further comprises a state adjustment unit configured to: The status returned by the processing of the corresponding file is obtained, and then the status of the corresponding polling object is adjusted according to the returned status.

8. A file processing electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 4.

9. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.