Attribute synchronization method and apparatus, storage medium, and electronic device
By using descriptors to monitor changes in object attributes in Python, the problems of server lag and code complexity caused by cross-process data synchronization in multi-process server architectures are solved, achieving efficient and clear attribute synchronization and improving code reusability and extensibility.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- NETEASE (HANGZHOU) NETWORK CO LTD
- Filing Date
- 2022-06-17
- Publication Date
- 2026-04-28
AI Technical Summary
In a multi-process server architecture, existing cross-process data synchronization solutions suffer from problems such as server lag, complex code, poor code reusability, and difficulty in maintaining multiple copies of the code.
This paper provides an attribute synchronization method by using descriptors in Python to listen for changes in object attributes, record the changed data, and broadcast it to other processes. It utilizes MRO order and stack frames to obtain objects of the class and bind descriptors to achieve attribute synchronization.
It improves attribute synchronization efficiency, reduces redundant data transmission, simplifies code maintenance, increases code reusability and extensibility, and is suitable for more application scenarios.
Smart Images

Figure CN115129485B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and more specifically, to an attribute synchronization method, apparatus, storage medium, and electronic device. Background Technology
[0002] In multi-process server architectures, it is often necessary to synchronize data across processes to ensure that each process can update data in a timely manner and maintain data consistency. This is especially true in game development scenarios, where the application of cross-process data synchronization is more widespread.
[0003] Currently, there are two approaches to cross-process data synchronization: full synchronization, which synchronizes all data (including modified and unchanged data) to all processes whenever any process performs an add, delete, query, or update operation; and incremental synchronization, which only synchronizes modified data to all processes whenever any process performs an add, delete, query, or update operation. The former approach can lead to data redundancy and excessive CPU consumption when dealing with large amounts of data and frequent synchronization, causing server lag. The latter approach requires maintaining multiple copies of the data synchronization code due to different business scenarios, resulting in complex code, poor reusability, and difficulty in maintenance.
[0004] There is currently no effective solution to the above problems. Summary of the Invention
[0005] At least some embodiments of the present invention provide an attribute synchronization method, apparatus, storage medium and electronic device to at least solve the technical problems in the related art of server lag, code complexity, poor code reusability and difficulty in maintaining multiple copies of code when synchronizing data across processes.
[0006] According to one embodiment of the present invention, an attribute synchronization method is provided, comprising:
[0007] The system receives a first request through a first function, wherein the first function is used to receive requests from multiple processes, and the first request is used to update a first attribute of a first object; updates the first attribute of the first object according to the first request, and generates first updated data, wherein the first attribute is a predefined attribute, and the first updated data is used to reflect the data change of the first attribute; obtains the first updated data through a second function, wherein the second function is used to obtain the updated data of the first attribute; and broadcasts the first updated data to multiple processes so that the multiple processes update the first updated data synchronously.
[0008] Optionally, it also includes: obtaining the filename attribute of the first class, wherein the first class is used to create the first object; binding the second class to the filename attribute of the first class, wherein the second class is used to listen for changes in the first attribute; and creating a third class based on the first constructor and the second constructor, wherein the first constructor is used to create the second class, the second constructor is used to create the third class, and the third class is used to store the attribute value of the first attribute.
[0009] Optionally, binding the second class to the filename attribute of the first class includes: calculating the method parsing order in the third constructor to obtain the target frame, wherein the third constructor is used to create the first class and the target frame is the frame instantiated from the second class; and binding the second class to the filename attribute of the first class according to the target frame.
[0010] Optionally, creating a third class based on the first constructor and the second constructor includes: calling the first constructor, where the first constructor includes a filename variable; calling the second constructor to create the third class and multiple objects of the third class, where the second constructor includes a filename variable and a default variable, and the multiple objects store the filename variable and the default variable, where the default variable is a number or a class.
[0011] Optionally, updating the first attribute of the first object according to the first request includes: in response to the absence of the first object, creating the first object and updating the first data of the first attribute to the second data; in response to the existence of the first object, reading the first data of the first attribute and updating the first data of the first attribute to the second data.
[0012] Optionally, it also includes: storing the third data in the first dictionary of the fourth class, wherein the third data is the difference data between the first data and the second data, and the fourth class is the base class of the first class.
[0013] Optionally, obtaining the first updated data through the second function includes: creating a second dictionary according to the second function, wherein the second dictionary is an empty dictionary; traversing the third data in the first dictionary; in response to the first attribute being a numeric attribute, saving the third data to the second dictionary; in response to the first attribute being a dictionary attribute or a nested attribute, calling the third function of the fourth class and the fourth function of the calling instance to change the first attribute to a numeric attribute, and saving the data corresponding to the changed numeric attribute to the second dictionary; and obtaining the data saved in the second dictionary to obtain the first updated data.
[0014] Optionally, it also includes: creating a fifth class that inherits from the fourth class, and calling the second function through the fifth class to obtain the first updated data, wherein the first attribute is a nested attribute.
[0015] Optionally, it also includes: creating a sixth class that inherits from the fifth class, and calling a second function through the sixth class to obtain the first updated data, wherein the first attribute is a dictionary attribute.
[0016] According to one embodiment of the present invention, an attribute synchronization device is also provided, comprising:
[0017] The system comprises: a receiving module for receiving a first request via a first function, wherein the first function receives requests from multiple processes, and the first request updates a first attribute of a first object; a processing module for updating the first attribute of the first object according to the first request and generating first updated data, wherein the first attribute is a predefined attribute, and the first updated data reflects the data changes of the first attribute; an obtaining module for obtaining the first updated data via a second function, wherein the second function obtains the updated data of the first attribute; and a broadcasting module for broadcasting the first updated data to multiple processes so that the multiple processes update the first updated data synchronously.
[0018] Optionally, the processing module is also used to obtain the filename attribute of the first class, wherein the first class is used to create the first object; bind the second class to the filename attribute of the first class, wherein the second class is used to listen for changes in the first attribute; and create the third class according to the first constructor and the second constructor, wherein the first constructor is used to create the second class, the second constructor is used to create the third class, and the third class is used to store the attribute value of the first attribute.
[0019] Optionally, the processing module is also used to calculate the method parsing order in the third constructor to obtain the target frame, wherein the third constructor is used to create the first class and the target frame is the frame instantiated from the second class; and the second class is bound to the filename attribute of the first class according to the target frame.
[0020] Optionally, the processing module is also used to call the first constructor, which includes a filename variable; and to call the second constructor to create a third class and multiple objects of the third class, wherein the second constructor includes a filename variable and a default variable, and the multiple objects store the filename variable and the default variable, the default variable being a number or a class.
[0021] Optionally, the processing module is further configured to, in response to the absence of a first object, create a first object and update the first data of the first attribute to the second data; and in response to the existence of a first object, read the first data of the first attribute and update the first data of the first attribute to the second data.
[0022] Optionally, the processing module is also used to store the third data in the first dictionary of the fourth class, wherein the third data is the difference data between the first data and the second data, and the fourth class is the base class of the first class.
[0023] Optionally, the acquisition module is also used to create a second dictionary based on the second function, wherein the second dictionary is an empty dictionary; traverse the third data in the first dictionary; in response to the first attribute being a numeric attribute, save the third data to the second dictionary; in response to the first attribute being a dictionary attribute or a nested attribute, call the third function of the fourth class and the fourth function of the calling instance to change the first attribute to a numeric attribute, and save the corresponding data after the change to a numeric attribute to the second dictionary; and obtain the data saved in the second dictionary to obtain the first updated data.
[0024] Optionally, the processing module is also used to create a fifth class that inherits from the fourth class, and to call the second function through the fifth class to obtain the first updated data, wherein the first attribute is a nested attribute.
[0025] Optionally, the processing module is also used to create a sixth class that inherits from the fifth class, and to call the second function through the sixth class to obtain the first updated data, wherein the first attribute is a dictionary attribute.
[0026] According to one embodiment of the present invention, a computer-readable storage medium is also provided, wherein the storage medium stores a computer program, wherein the computer program is configured to execute the attribute synchronization method described above when running on a computer or processor.
[0027] According to one embodiment of the present invention, an electronic device is also provided, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the attribute synchronization method described in any of the preceding claims.
[0028] In at least some embodiments of the present invention, a first request for updating a first attribute of a first object is received through a first function, the first attribute of the first object is updated according to the first request, and first updated data is generated. The first attribute is a predefined attribute. The first updated data is obtained through a second function and broadcast to multiple processes so that multiple processes can update the first updated data synchronously. This makes the attribute definition clearer, more intuitive and inheritable, makes the solution applicable to more application scenarios, increases reusability and scalability, reduces the sending of redundant data, improves attribute synchronization efficiency, and eliminates the need to maintain additional synchronization code, thus improving development efficiency. This solves the technical problems in related technologies where server lag, code complexity, poor code reusability, and difficulty in maintaining multiple copies of code are common when synchronizing data across processes. Attached Figure Description
[0029] The accompanying drawings, which are included to provide a further understanding of the invention and form part of this application, illustrate exemplary embodiments of the invention and, together with their description, serve to explain the invention and do not constitute an undue limitation thereof. In the drawings:
[0030] Figure 1 This is a hardware structure block diagram of a mobile terminal according to an embodiment of the present invention, which describes an attribute synchronization method.
[0031] Figure 2 This is a flowchart of an attribute synchronization method provided according to an embodiment of the present invention;
[0032] Figure 3 This is a flowchart of obtaining the first updated data in an attribute synchronization method provided by an embodiment of the present invention;
[0033] Figure 4 This is a class diagram of an attribute synchronization method provided according to an embodiment of the present invention;
[0034] Figure 5 This is a schematic diagram of the attribute synchronization device according to an embodiment of the present invention;
[0035] Figure 6 This is a hardware structure block diagram of an electronic device for an attribute synchronization method according to an embodiment of the present invention. Detailed Implementation
[0036] For ease of understanding, some concepts related to embodiments of the present invention are illustrated below for reference.
[0037] Multi-process server architecture: refers to a server software architecture that increases server processing power by building a server using multiple processes.
[0038] Python: An object-oriented interpreted computational programming language.
[0039] Descriptor: A descriptor is essentially a class. In the descriptor protocol, it allows overriding property access through methods. By binding descriptors, properties of other classes can be managed by the descriptor class; this can be understood as listening to and intercepting properties.
[0040] Stack frame: Used to represent the context of code execution. Typically, when code jumps in the Python virtual machine, the context of the currently running code is pushed onto the stack.
[0041] Method resolution order (MRO) represents the order of class inheritance and the execution order of overloaded methods in Python. In this embodiment of the invention, the MRO order is calculated in the class constructor to obtain the frame sequence during class construction. The constructor is recursively called according to the MRO order, and the recursive call pushes frames onto the stack level by level.
[0042] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0043] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0044] In multi-process server architectures, cross-process data synchronization is frequently required. For example, in a game's team system, the team object and each member may reside in different processes. Therefore, it's necessary to ensure that when team data changes (which can be understood as performing CRUD operations on team members, such as adding a new member), every member is notified. In other words, each process needs to continuously update the latest team data to maintain synchronization. Another example is in a server-wide monster-hunting gameplay mode. Monsters are controlled by a single central process, while players across the server reside in multiple other processes. These other processes need to synchronously obtain information such as monster health. Therefore, when monster data changes (which can be understood as performing CRUD operations on monsters, such as reducing monster health), the monster data needs to be synchronized with these other processes.
[0045] To achieve cross-process data synchronization, two main approaches exist: Full synchronization and Incremental synchronization. Full synchronization requires all processes to share all data (including modified and unchanged data) whenever any process performs an add, delete, query, or update operation. For example, in the game's team system, when operations such as a member joining, leaving, or changing the team leader are performed, a full synchronization code needs to be added after the operation code to synchronize all team data to all processes. However, with large amounts of data, full synchronization can lead to redundancy and excessive CPU consumption, causing server lag. Incremental synchronization only synchronizes modified data to all processes when any process performs an add, delete, query, or update operation. For instance, in the game's team system, when a member joins a team, an incremental synchronization code is added after the operation code. This code calculates the changes in team member data and synchronizes only the changed data to all processes. However, since different add, delete, query, and update operations require different incremental synchronization codes, this approach requires maintaining multiple incremental synchronization codes, resulting in complex, difficult-to-maintain, and poorly reusable code.
[0046] To address the aforementioned technical problems, this invention uses Python as the development programming language. It predefines and sets attributes for class objects (teams, members, and monsters can all be considered objects). The invention obtains class objects through MRO (Mean Relationship Optimization) and stack frames, binds them to descriptors, and uses the Python descriptor to monitor attribute changes. When an attribute changes, it records the changes and finally retrieves the changed attribute data by calling a function interface, broadcasting the data to other processes to achieve attribute synchronization. This invention provides a nestable and inheritable attribute definition and change maintenance scheme, making attribute definitions clearer, more intuitive, and inheritable. This increases the reusability and extensibility of the scheme, making it suitable for more application scenarios. It also reduces the transmission of redundant data, improves attribute synchronization efficiency, and eliminates the need to maintain additional synchronization code, thus improving development efficiency.
[0047] According to one embodiment of the present invention, an embodiment of an attribute synchronization method is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0048] This method embodiment can be executed on a mobile terminal, computer terminal, or similar computing device. Taking running on a mobile terminal as an example, the mobile terminal can be a smartphone (such as an Android phone, iOS phone, etc.), tablet computer, PDA, mobile internet device (MID), tablet computer (PAD), game console, and other terminal devices. Figure 1 This is a hardware structure block diagram of a mobile terminal for an attribute synchronization method according to an embodiment of the present invention. Figure 1 As shown, a mobile terminal may include one or more ( Figure 1 Only one is shown in the diagram. A processor 102 and a memory 104 for storing data are also included. Optionally, the mobile terminal may further include a transmission device 106 for communication functions, an input / output device 108, and a display device 110. Those skilled in the art will understand that... Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the mobile terminal described above. For example, the mobile terminal may also include components that are more... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown.
[0049] The processor 102 may include, but is not limited to, processing devices such as central processing unit (CPU), graphics processing unit (GPU), digital signal processing (DSP) chip, microcontroller unit (MCU), field-programmable gate array (FPGA), neural network processing unit (NPU), tensor processing unit (TPU), and artificial intelligence (AI) type processors.
[0050] The memory 104 can be used to store computer programs, such as application software programs and modules, like the computer program corresponding to the attribute synchronization method in this embodiment of the invention. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, thereby realizing the aforementioned attribute synchronization method. The memory 104 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to the mobile terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0051] The transmission device 106 is used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by the mobile terminal's communication provider. In one example, the transmission device 106 includes a network interface controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 may be a radio frequency (RF) module used for wireless communication with the Internet.
[0052] The inputs in input / output device 108 can come from multiple human interface devices (HIDs). Examples include keyboards and mice, game controllers, and other dedicated game controllers (such as steering wheels, fishing rods, dance mats, and remote controls). Some HIDs, in addition to providing input functions, can also provide output functions, such as force feedback and vibration from game controllers, and audio output from controllers.
[0053] Display device 110 may be, for example, a head-up display (HUD), a touchscreen liquid crystal display (LCD), or a touch display (also referred to as a "touchscreen" or "touch display"). This LCD allows a user to interact with the user interface of the mobile terminal. In some embodiments, the mobile terminal has a graphical user interface (GUI), which allows the user to interact with the GUI through finger contact and / or gestures on a touch-sensitive surface. Optional human-computer interaction functions include: creating web pages, drawing, word processing, creating electronic documents, playing games, video conferencing, instant messaging, sending and receiving emails, a call interface, playing digital video, playing digital music, and / or web browsing, etc. Executable instructions for performing the above human-computer interaction functions are configured / stored in one or more processor-executable computer program products or readable storage media.
[0054] Under the above operating environment, the present invention provides, as follows: Figure 2 The property synchronization method shown can be used by... Figure 1 The mobile terminal or similar electronic device shown is used for execution. Figure 2 This is a flowchart of an attribute synchronization method provided according to an embodiment of the present invention. Figure 2 As shown, the method may include the following steps:
[0055] Step S201: Receive the first request through the first function.
[0056] The first function is used to receive requests from multiple processes. It can be understood as defining a first function to receive requests from other processes and receiving requests from other processes through this first function.
[0057] The first request is used to update the first attribute of the first object. This can be understood as a request received from other processes to update the attribute of any object. Updating the attribute includes operations such as adding, deleting, searching, and modifying the attribute.
[0058] For example, taking the first function as the receive function, the first object as the team object, and the first property as the length property, the above steps can be understood as defining the receive function and receiving the first request from other processes through the receive function. This first request is used to update the length of the team object, that is, to indicate that a member has joined or left the team.
[0059] Step S202: Update the first attribute of the first object according to the first request and generate the first updated data.
[0060] Among them, the first attribute is a predefined attribute, which can be understood as the attribute of the first object being defined in advance.
[0061] The first updated data is used to reflect the data changes of the first attribute. It can be understood as the difference between the data before the first attribute was updated and the data after the first attribute was updated.
[0062] For example, the length of the team object is updated according to the first request, and the difference data before and after the length update of the team object is generated.
[0063] Step S203: Obtain the first updated data through the second function.
[0064] The second function is used to obtain the updated data of the first attribute, which can be understood as the difference between the data before the first attribute was updated and the data after the first attribute was updated.
[0065] For example, taking the second function as obj.consume_dirty(), where obj refers to the variable name of the object, when the first object is a team object, the second function is team.consume_dirty(). By calling team.consume_dirty(), the difference in length of the team object before and after the update can be obtained.
[0066] Step S204: Broadcast the first update data to multiple processes so that the multiple processes can update the first update data synchronously.
[0067] For example, after obtaining the first updated data, the first updated data is broadcast to multiple processes through the broadcast interface of the receive function. After receiving the broadcast first updated data, the multiple processes will update the first updated data synchronously to ensure that the first attribute is updated synchronously.
[0068] Through the above steps, a first request for updating the first attribute of a first object is received through a first function, the first attribute of the first object is updated according to the first request, and first updated data is generated. Here, the first attribute is a predefined attribute. The first updated data is obtained through a second function and broadcast to multiple processes so that multiple processes can update the first updated data synchronously. This makes the attribute definition clearer, more intuitive and inheritable, makes the solution applicable to more application scenarios, increases reusability and scalability, reduces the sending of redundant data, improves attribute synchronization efficiency, and eliminates the need to maintain additional synchronization code, thus improving development efficiency. This solves the technical problems in related technologies where server lag, code complexity, poor code reusability, and difficulty in maintaining multiple copies of code are common when synchronizing data across processes.
[0069] Optionally, before receiving the first request through the first function in step S201, the following execution steps may also be included:
[0070] Step S2011: Obtain the file name attribute of the first type.
[0071] Step S2012: Bind the second type to the filename attribute of the first type.
[0072] The first type is used to create the first object, and the second type is used to listen for changes in the first property.
[0073] For example, taking the first type as the Team class and the second type as the PropBase class, the object created by the Team class is a team object, and the file name attribute is the name attribute. Here, PropBase can be understood as a descriptor. By binding PropBase to the name attribute of the Team class, changes to the team object's attributes can be monitored through PropBase.
[0074] Step S2013: Create the third class based on the first constructor and the second constructor.
[0075] The first constructor is used to create the second class, the second constructor is used to create the third class, and the third class is used to store the attribute value of the first attribute.
[0076] Since the descriptor only intercepts read and write requests for properties and does not store property values, a third class needs to be created to store the property values of the first property. Creating the third class based on the first constructor of the second class allows the created third class to inherit from the second class; this can be understood as the third class being indirectly bound to the `name` property of the `Team` class.
[0077] For example, taking the third type, NormalProp, as an example, NormalProp is created according to the constructor (__init__) of PropBase and NormalProp, making NormalProp a subclass of PropBase, that is, PropBase is the base class of NormalProp.
[0078] Steps S2011-S2013 above can be understood as follows: Before step S201, all modules and classes need to be initialized. During the initialization of the Team class, the name attribute of the Team class is bound through PropBase. Then, NormalProp is created to inherit from PropBase, thereby binding NormalProp to the name attribute of the Team class. These steps can be understood as predefining the attributes of the first type.
[0079] Optionally, in step S2012, binding the second class to the filename attribute of the first class may include the following steps:
[0080] Step S2012a: Calculate the method parsing order in the third constructor to obtain the target frame.
[0081] The third constructor is used to create the first class, with the target frame being a frame instantiated from the second class.
[0082] Since the constructor is recursively called according to the MRO order, the recursive call will push the frames onto the stack level by level. Therefore, calculating the MRO order in the constructor of the first type can obtain the sequence of frames pushed onto the stack level by level during the construction of the first type. The frame instantiated by the second type is the last_frame in the frame sequence.
[0083] Step S2012b: Bind the second type to the filename attribute of the first type according to the target frame.
[0084] Since the binding essentially sets the filename attribute and descriptor object into the class's dictionary (__dict__), in this embodiment of the invention, it is to set the name attribute and PropBase into the Team class's dictionary (Team.__dict__). last_frame.f_locals is the aforementioned __dict__, and f_locals is the local space of the frame, equivalent to Cls.__dict__, which is where class attributes are stored. In this embodiment of the invention, Team.__dict__ is where Team class attributes are stored.
[0085] For example, in the constructor of PropBase, the MRO sequence frame_seq is calculated, and the frame at the time of PropBase construction is obtained using last_frame = sys._getframe(frame_seq). Then, based on last_frame.f_locals[name] = self, PropBase is bound to the name attribute of the Team class. Here, last_frame.f_locals[name] = self is equivalent to Team.var_name = PropBase(“var_name”), which can be understood as binding PropBase to the name attribute of the Team class based on last_frame, thereby completing the descriptor binding.
[0086] Optionally, in step S2013, creating the third class based on the first constructor and the second constructor may include the following execution steps:
[0087] Step S2013a: Call the first constructor.
[0088] Step S2013b: Call the second constructor to create the third class and multiple objects of the third class.
[0089] The first constructor includes a filename variable (name variable), the second constructor includes a filename variable and a default variable (default variable), and multiple objects of the third class store the filename variable and the default variable, where the default variable is a number or a class.
[0090] Since the descriptor is stored in the class property and only intercepts read and write requests for the property, it does not store the property value. Therefore, the property value needs to be stored in the instance, specifically the `name` and `default` variables stored by the `NormalProp` object. When the `default` variable is a number, it represents the default property value of the `name` variable. When the `default` variable is a class, it represents a nested class.
[0091] For example, first call the constructor PropBase.__init__ of PropBase, passing in the name variable, and then call the constructor NormalProp.__init__ of NormalProp, passing in the name variable name and the default value, to create NormalProp and multiple NormalProp objects. The NormalProp objects store the name variable and the default variable.
[0092] Optionally, in step S202, updating the first attribute of the first object according to the first request may include the following execution steps:
[0093] Step S2021: In response to the absence of a first object, create a first object and update the first data of the first attribute to the second data.
[0094] When the first object does not exist, the first object is created. At this time, the first data of the first attribute can be understood as empty, and the first attribute of the created first object is the second data.
[0095] For example, in this embodiment of the invention, since the first class (Team class) inherits from the fourth class (SyncBase class), when creating the first object, the constructor SyncBase.__init__ of SyncBase is called first, and then the constructor Team.__init__ of Team class is called to create an object of Team class, i.e., the first object.
[0096] Step S2022: In response to the existence of a first object, read the first data of the first attribute and update the first data of the first attribute to the second data.
[0097] When the first object exists, first read the first data of the first attribute, perform a read operation on the first attribute, and then perform a write operation on the first attribute to update the first data to the second data.
[0098] For example, let's take the first class as `Team`, the first object as `team`, and the first attribute as `x`. During a read operation, when reading the `x` attribute of the `team` object, since the `x` attribute of the `Team` class is already bound to `NormalProp`, and the `Team` class has declared the `NormalProp` corresponding to the `x` attribute (here, the corresponding `NormalProp` object is called `x_prop`), according to Python's language features. If a descriptor is bound when reading the attribute, the descriptor's `__get__` function will be called, i.e., `x_prop.__get__`, automatically passing in the parameters `instance` and `cls`. In this case, the `instance` parameter is `team`, and the `cls` parameter is `Team`.
[0099] In `x_prop.__get__`, an instance property name (`__x`) is created based on the previously saved `name` (i.e., `x`), called `real_name`. It then checks if `instance.__dict__` contains `real_name`. If it does, it returns the value stored in `instance.__dict__`, i.e., `instance.__dict__[real_name]`. If it doesn't, it calculates a default value and assigns it to `instance.__dict__[real_name]`.
[0100] When calculating the default value, if `default` is a class, a default value object is constructed using `default` (since `default` is a class, you can directly construct an object of that class in Python). The constructor takes the parameters `name` and `instance` as input, and the default value is the constructed object. Otherwise, if `default` is not a class but a number, the default value is simply `default`.
[0101] Furthermore, when `default` is a class, it can be a subclass of any `CustomDict` (i.e., a nested class), such as the `DictProp` class (i.e., a dictionary class) or the `InheritProp` class (i.e., an inherited class). Since `default` is a subclass of `CustomDict`, the constructor `CustomDict.__init__` of `CustomDict` is called when constructing the default value object. The constructor stores the passed-in `name` and `instance` variables, where `name` is the name declared in `NormalProp`, and `instance` is an object of the class in which `NormalProp` is declared (for example, if `NormalProp` is declared in the `Team` class, then `instance` is an object of the `Team` class; if `NormalProp` is declared in the `InheritProp` class, then `instance` is an object of the `InheritProp` class). In this embodiment of the invention, since `CustomDict` is also a subclass of `SyncBase`, `SyncBase.__init__(name)` is also executed when `CustomDict` is created.
[0102] For write operations, it's necessary to distinguish the type of the first attribute, which falls into three categories: numeric attributes (ordinary attributes, whose default values are integers, floating-point numbers, or strings), dictionary attributes, and nested attributes. When the first attribute is a ordinary attribute, i.e., the attribute 'x' is an ordinary attribute, a value 'write_value' is written to the attribute 'x' according to 'team.x = write_value'. Due to Python's language features, when writing an attribute, if a descriptor is bound, the descriptor's '__set__' function will be called, i.e., 'x_prop.__set__', automatically passing in the parameters 'instance' and 'value'. In this case, the 'instance' parameter is 'team', and the 'value' parameter is 'write_value'.
[0103] In `x_prop.__set__`, an instance property name (i.e., `__x`) is created based on the previously saved `name` (i.e., `x`), called `real_name`. The value is set in `instance.__dict__[real_name]`, and `instance.set_dirty(name, value)` is called. Since `instance` inherits from `SyncBase`, `set_dirty` is `SyncBase.set_dirty`. In `SyncBase.set_dirty`, `name` and `value` are saved as keys and values to the dictionary `dirty_dict`, completing the write operation.
[0104] When the first attribute is a dictionary attribute (i.e., the x attribute is a dictionary attribute), if the Team class declares the attribute user_names, whose default value is the DictProp class, then according to team.user_names[uid] = new_name, uid and new_name are set as the key and value respectively into the team.user_names dictionary to complete the write operation. In Python, writing attributes is done in two parts: first, the team's user_names attribute is read, and then user_names[uid] = new_name is executed.
[0105] Reading the `team.user_names` property follows the same steps as the read operation described above, so we won't go into detail here. The only difference is that the read `user_names` is a `DictProp` object, and its `name` is `user_names` (since `DictProp` inherits from `CustomDict`, it has a `name`). Next, `user_names[uid] = new_name` is executed, assigning a value to the dictionary and calling the function `user_names.__setitem__(key, value)`, where `uid` and `new_name` are the key and value, respectively. Within this function, `dict.__setitem__(key, value)` is first called to save the key-value pair `uid` and `new_name`, and then `self.set_dirty(key, value)` is called. Since `DictProp` inherits from `CustomDict`, `set_dirty` is now equivalent to `CustomDict.set_dirty`.
[0106] Finally, `CustomDict.set_dirty` is called, passing in a key and a value. Specifically, the parent class's (i.e., `SyncBase`) `set_dirty` is called first, passing in the key and value. At this point, the change in the `uid` and `new_name` attributes is recorded in the `user_names` attribute. Then, `CustomDict.instance.set_dirty` is called, passing in `name` and `self` (in Python, `self` is the current object, i.e., `user_names`). Here, `CustomDict.instance` is passed in when `CustomDict` is constructed (i.e., passed in during the read operation above when `default` is a class). In this embodiment, `CustomDict.instance` is `team`. `name` is passed in when `CustomDict` is constructed, and its value is `user_names`. This step is to notify the `team` of the change in the `user_names` attribute, so that the `team` knows that the `user_names` attribute has been updated.
[0107] When the first attribute is a nested attribute (i.e., the 'x' attribute is a nested attribute), if the Team class declares the attribute 'inherit' with a default value of the InheritProp class, then the write operation is completed according to team.inherit.var = new_value. Similar to the write operation on dictionary attributes described above, the team's 'inherit' attribute is read first, and then inheritit.var = new_value is executed.
[0108] Reading the `team.inherit` property follows the same steps as the read operation described above, so we won't go into detail here. The only difference is that the `inherit` returned is an `InheritProp` object. The difference is that if the `team.inherit` property hasn't been read before, the default value constructor (default value calculation) from the read operation above will be executed. During construction, the `name` and `instance` variables are passed in. In this case, `inherit.name` is `inherit`, and `inherit.instance` is `team`. When executing `inherit.var = new_value`, most operations are the same as when the first property is a regular property. Because `InheritProp` inherits from `CustomDict`, and `CustomDict` inherits from `SyncBase`, it will execute `SyncBase.__set__(instance, value)`, where `instance` is the `inherit` object, and `value` is `new_value`. The difference is that the instance object is not the team object, but inherit, and inherit is the InheritProp object. Since InheritProp inherits from CustomDict, the set_dirty function called is CustomDict.set_dirty instead of SyncBase.set_dirty.
[0109] Finally, `CustomDict.set_dirty` is called, which first calls `SyncBase.set_dirty`, passing in a key and a value. Here, the key is `var`, and the value is `new_value`. This records the attribute change for `inherit`, specifically the change from the `var` attribute to `new_value`. Then, `CustomDict.instance.set_dirty` is called, passing in `name` (i.e., `inherit`) and `self` (i.e., `inherit` itself). At this point, `CustomDict.instance` becomes `inherit.instance`, storing the `team` object (because `inherit`'s `NormalProp` is declared in the `Team` class). This step is to notify the team that its `inherit` attribute has changed.
[0110] Since nesting can be multi-level, for ease of understanding, this section only introduces the case of single-level nesting, where Team has an inherit property and inherit has a var property. In reality, there are multiple levels of nesting, but we will not go into detail here.
[0111] Optionally, the following execution steps may also be included:
[0112] Step S2023: Save the third data in the first dictionary of the fourth category.
[0113] The third data is the difference between the first and second data, and the fourth category is the base class of the first category. This can be understood as follows: after reading or writing to the first attribute of the team object, the value written during the write operation on the first attribute is saved as the third data in the first dictionary. The written value can be understood as the difference between the first and second data; that is, the first dictionary is used to store the difference data of the first attribute, and the fourth category is used to store the difference data of the first attribute.
[0114] For example, taking the fourth category as SyncBase, and the first dictionary of the fourth category as Obj.dirty_dict. After updating the first attribute, the difference data before and after the update of the first attribute will be saved to Obj.dirty_dict of SyncBase.
[0115] Specifically, when creating the fourth class (SyncBase), a dictionary-type member variable named `dirty_dict` is used to store the attribute change information of the first class. This can be understood as `dirty_dict` being used to store the third type of data. The `SyncBase.set_dirty(key, value)` function is provided to set attribute changes, directly setting key-value pairs into `dirty_dict`. Furthermore, the `SyncBase.consume_dirty()` function is provided, which returns a dictionary of attribute changes and clears the attribute changes. This can be understood as `SyncBase.consume_dirty()` being used to retrieve the first update information for the first attribute.
[0116] When implementing the `SyncBase.consume_dirty()` function, it first declares the variable `dirty = {}`, which creates a new dictionary (attribute change dictionary) to store attribute change information. Then, it iterates through all the key-value pairs stored in `dirty_dict`, which can be understood as iterating through all the third data stored in `dirty_dict` and determining whether the third data is a `SyncBase` object. If the value is not a `SyncBase` object, it means that the value is a single value (integer, floating-point number, or string), and then `value.consume_dirty()` is written to `dirty[key]`. If the value is a `SyncBase` object, it means that the value is a nested attribute or a dictionary attribute, and then `value` is written to `dirty[key]`.
[0117] Understandably, any class that inherits from SyncBase can declare the NormalProp property and use the consume_dirty function to obtain property changes of the Team class.
[0118] Optionally, in step S203, obtaining the first updated data through the second function may include the following execution steps:
[0119] Step S2031: Create a second dictionary based on the second function.
[0120] The second dictionary is an empty dictionary used to store the first updated data of the first attribute, and is therefore also called the attribute change dictionary.
[0121] For example, the second function obj.consume_dirty() is called. In this embodiment of the invention, obj.consume_dirty() is team.consume_dirty(). The second function will first create a new empty dictionary to store the first updated data of the first attribute. This empty dictionary is also called the attribute change dictionary, that is, the attribute change dictionary is created to store the first updated data of the first attribute.
[0122] Step S2032: Traverse the third data in the first dictionary.
[0123] Referring to the description in step S2023, the third data in the first dictionary is traversed to determine whether the third data is a SyncBase object. Since the third data reflects the attribute type of the first attribute, when the third data is not a SyncBase object, it means the value is a single value (integer, floating-point number, or string), which can be understood as the first attribute being a numeric attribute, i.e., a regular attribute. When the third data is a SyncBase object, it means the value is a nested attribute or a dictionary attribute, which can be understood as the first attribute being a nested attribute or a dictionary attribute. Therefore, depending on the type of the first attribute, the subsequent method of obtaining the first updated data will also differ.
[0124] Step S2033: In response to the first attribute being a numeric attribute, save the third data into the second dictionary.
[0125] When the first attribute is a normal attribute, the third data stored in the first dictionary is the difference data before and after the first attribute is updated, that is, the first update data. Therefore, the third data can be stored in the attribute change dictionary.
[0126] Step S2034: In response to the first attribute being a dictionary attribute or a nested attribute, the third function of the fourth class and the fourth function of the instance are called to change the first attribute to a numeric attribute, and the corresponding data after the change to a numeric attribute is saved to the second dictionary.
[0127] When the first attribute is a dictionary attribute or a nested attribute, to obtain the first updated data for the first attribute, the third function `set_dirty` of the fourth category needs to be called first to record the change in the first attribute. Then, the fourth function `instance.set_dirty` of the instance needs to be called so that the instance or the parent attribute can be aware of the change in the child attribute (the current attribute). Therefore, by recursively assigning `value = value.consume_dirty()`, the first attribute can be recursively converted to a regular attribute, which is equivalent to changing the first attribute from a dictionary attribute or nested attribute to a regular attribute. The data corresponding to the first attribute after being changed to a regular attribute is saved in the second dictionary, and the data corresponding to the first attribute after being changed to a regular attribute is the first updated data.
[0128] Step S2035: Obtain the data stored in the second dictionary to get the first updated data.
[0129] After the traversal is complete, the values stored in the second dictionary represent the differences between the first attribute before and after the update, i.e., the first updated data. By retrieving the first updated data from the second dictionary and broadcasting it to multiple processes, the processes can synchronously update the first updated data, thus completing attribute synchronization.
[0130] For example, such as Figure 3 As shown, Figure 3 The flowchart for steps S2031-S2035, obtaining the first updated data, is as follows: First, the second function is called. Based on `obj.consume_dirty()`, an empty dictionary, `dirty`, is created to store the first updated data for the first attribute. Next, the first dictionary `Obj.dirty_dict` in the fourth type of `SyncBase` is traversed, checking if the third data in `Obj.dirty_dict` is a `SyncBase` object. If the third data is not a `SyncBase` object, it is saved to `dirty`. If the third data is a `SyncBase` object, it is recursively assigned the value `value = value.consume_dirty()`, recursively converting the first attribute from a dictionary attribute or nested attribute to a regular attribute, and saving the corresponding data after changing it to a numeric attribute to `dirty`. After the traversal is complete, the data saved in `dirty` is retrieved, yielding the first updated data.
[0131] Optionally, the following execution steps may also be included:
[0132] Step S205: Create a fifth class that inherits from the fourth class, and use the fifth class to call the second function to obtain the first updated data.
[0133] The first attribute is a nested attribute.
[0134] For example, let's take the fifth category, CustomDict, as an example. Create a class CustomDict that inherits from SyncBase. In the constructor, it receives name and instance variables and stores weak references to name and instance. It overrides the set_dirty function of the parent class SyncBase, receiving key and value parameters. Specifically, it first calls SyncBase's set_dirty(key, value) to record the current attribute change, and then calls instance's set_dirty(name, self) so that the instance or parent attribute can perceive the change in the child attribute (current attribute).
[0135] The attribute definition in this embodiment of the invention has good extensibility and supports nested definitions, which makes the attribute definition flexible and applicable to a wider range of scenarios.
[0136] Optionally, the following execution steps may also be included:
[0137] Step S206: Create a sixth class that inherits from the fifth class, and use the sixth class to call the second function to obtain the first updated data.
[0138] The first attribute is a dictionary attribute.
[0139] For example, let's take the sixth category, DictProp, as an example. The DictProp class inherits from CustomDict and dict, providing the __setitem__, __delitem__, pop, and clear functions. The implementation logic of these functions is similar. Specifically, calling the native dict calls dict.__setitem__(self, key, value); calling the __setitem__ function calls self.set_dirty(key, value); calling __delitem__ and pop functions calls self.set_dirty(key, None); and calling the clear function requires iterating through all current keys and executing self.set_dirty(key, None).
[0140] Understandably, if an `InheritProp` class is declared, inheriting from `CustomDict`, and `InheritProp` also declares multiple `NormalProp` properties (i.e., the same as the `Team` class), then `InheritProp` will also be initialized. During this initialization process, the `Team` class will be used as the team object, while `InheritProp` will be used as a nested property. Since they both inherit from `SyncBase`, the initialization process for `NormalProp` is the same. Furthermore, `InheritProp` can be used as the default value of the `NormalProp` declared by `Team`, thus giving the `Team` class its own nested `InheritProp` property.
[0141] The attribute definition in this embodiment of the invention has good extensibility, supporting scalable nested definitions and dictionary type definitions, thereby making the attribute definition flexible and applicable to a wider range of scenarios.
[0142] For example, Figure 4 A class diagram diagram of an embodiment of the present invention is shown. It can be seen that the second type of PropBase is bound to the attributes of the first type of Team, thus enabling the second type of PropBase to listen to and intercept the attributes of the first type of Team. The third type of NormalProp inherits from the second type of PropBase, thus the third type of NormalProp can also listen to and intercept the attributes of the first type of Team, and is also used to store the attribute values of the first type of Team. The first type of Team inherits from the fourth type of SyncBase. The fourth type of SyncBase can set attribute changes for the first type of Team through the set_dirty function, and can also obtain the attribute change dictionary for the first type of Team through the consume_dirty() function. Furthermore, any class inheriting from the fourth type of SyncBase can obtain the attribute change dictionary for the first type of Team. In addition, the fifth type of CustomDict (nested class) inherits from the fourth type, and the sixth type of DictProp (dictionary class) inherits from the fifth type of CustomDict (nested class), enabling flexible attribute definitions and a wider range of application scenarios.
[0143] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods described in the various embodiments of the present invention.
[0144] This embodiment also provides an attribute synchronization device for implementing the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can refer to a combination of software and / or hardware that performs a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, hardware implementations, or a combination of software and hardware, are also possible and contemplated.
[0145] Figure 5 This is a structural block diagram of an attribute synchronization device according to one embodiment of the present invention, such as... Figure 5 As shown, the attribute synchronization device 500 includes: a receiving module 501, configured to receive a first request through a first function, wherein the first function is configured to receive requests from multiple processes, and the first request is configured to update a first attribute of a first object; a processing module 502, configured to update the first attribute of the first object according to the first request and generate first update data, wherein the first attribute is a predefined attribute, and the first update data is configured to reflect data changes of the first attribute; an obtaining module 503, configured to obtain the first update data through a second function, wherein the second function is configured to obtain the update data of the first attribute; and a broadcasting module 504, configured to broadcast the first update data to multiple processes so that the multiple processes synchronously update the first update data.
[0146] Optionally, the processing module 502 is further configured to obtain the filename attribute of the first class, wherein the first class is used to create the first object; bind the second class to the filename attribute of the first class, wherein the second class is used to listen for changes in the first attribute; and create a third class based on the first constructor and the second constructor, wherein the first constructor is used to create the second class, the second constructor is used to create the third class, and the third class is used to store the attribute value of the first attribute.
[0147] Optionally, the processing module 502 is also used to calculate the method parsing order in the third constructor to obtain the target frame, wherein the third constructor is used to create the first class and the target frame is the frame instantiated from the second class; and the second class is bound to the filename attribute of the first class according to the target frame.
[0148] Optionally, the processing module 502 is also used to call the first constructor, wherein the first constructor includes a filename variable; call the second constructor to create a third class and multiple objects of the third class, wherein the second constructor includes a filename variable and a default variable, and the multiple objects store the filename variable and the default variable, wherein the default variable is a number or a class.
[0149] Optionally, the processing module 502 is further configured to, in response to the absence of a first object, create a first object and update the first data of the first attribute to the second data; and in response to the existence of a first object, read the first data of the first attribute and update the first data of the first attribute to the second data.
[0150] Optionally, the processing module 502 is further configured to store the third data in the first dictionary of the fourth class, wherein the third data is the difference data between the first data and the second data, and the fourth class is the base class of the first class.
[0151] Optionally, the acquisition module 503 is further configured to create a second dictionary based on the second function, wherein the second dictionary is an empty dictionary; traverse the third data in the first dictionary; in response to the first attribute being a numeric attribute, save the third data to the second dictionary; in response to the first attribute being a dictionary attribute or a nested attribute, call the third function of the fourth class and the fourth function of the calling instance to change the first attribute to a numeric attribute, and save the corresponding data after the change to a numeric attribute to the second dictionary; and acquire the data saved in the second dictionary to obtain the first updated data.
[0152] Optionally, the processing module 502 is also used to create a fifth class that inherits from the fourth class, and to obtain the first updated data by calling the second function through the fifth class, wherein the first attribute is a nested attribute.
[0153] Optionally, the processing module 502 is also used to create a sixth class that inherits from the fifth class, and to obtain the first updated data by calling the second function through the sixth class, wherein the first attribute is a dictionary attribute.
[0154] It should be noted that the above modules can be implemented by software or hardware. For the latter, they can be implemented in the following ways, but are not limited to: all the above modules are located in the same processor; or, the above modules are located in different processors in any combination.
[0155] Embodiments of the present invention also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to perform the steps in any of the above method embodiments when run on a computer or processor.
[0156] Optionally, in this embodiment, the computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.
[0157] Optionally, in this embodiment, the computer-readable storage medium may be located in any computer terminal in a group of computer terminals in a computer network, or in any mobile terminal in a group of mobile terminals.
[0158] Optionally, in this embodiment, the computer-readable storage medium may be configured to store a computer program for performing the following steps: receiving a first request through a first function; updating a first attribute of a first object according to the first request to generate first updated data; obtaining the first updated data through a second function; and broadcasting the first updated data to multiple processes so that the multiple processes synchronously update the first updated data.
[0159] Optionally, the aforementioned computer-readable storage medium is further configured to store program code for performing the following steps: obtaining the filename attribute of the first class; binding the second class to the filename attribute of the first class; and creating the third class according to the first constructor and the second constructor.
[0160] Optionally, the aforementioned computer-readable storage medium is further configured to store program code for performing the following steps: calculating the method parsing order in the third constructor to obtain the target frame; and binding the second class to the filename attribute of the first class based on the target frame.
[0161] Optionally, the aforementioned computer-readable storage medium is further configured to store program code for performing the following steps: calling a first constructor; calling a second constructor to create a third class and multiple objects of the third class.
[0162] Optionally, the aforementioned computer-readable storage medium is further configured to store program code for performing the following steps: in response to the absence of a first object, creating a first object and updating first data of a first attribute to second data; in response to the existence of a first object, reading first data of a first attribute and updating first data of a first attribute to second data.
[0163] Optionally, the aforementioned computer-readable storage medium is further configured to store program code for performing the following steps: saving the third data in a first dictionary of the fourth class.
[0164] Optionally, the aforementioned computer-readable storage medium is further configured to store program code for performing the following steps: creating a second dictionary according to a second function; traversing the third data in the first dictionary; saving the third data to the second dictionary in response to the first attribute being a numeric attribute; calling the third function of the fourth class and the fourth function of the calling instance in response to the first attribute being a dictionary attribute or a nested attribute, changing the first attribute to a numeric attribute, and saving the data corresponding to the changed numeric attribute to the second dictionary; retrieving the data saved in the second dictionary to obtain the first updated data.
[0165] Optionally, the aforementioned computer-readable storage medium is further configured to store program code for performing the following steps: creating a fifth class that inherits from the fourth class, and obtaining first updated data by calling a second function through the fifth class.
[0166] Optionally, the aforementioned computer-readable storage medium is further configured to store program code for performing the following steps: creating a sixth class that inherits from the fifth class, and obtaining first updated data by calling a second function through the sixth class.
[0167] In this embodiment, a computer-readable storage medium provides a technical solution for attribute synchronization. A first function receives a first request to update a first attribute of a first object, updates the first attribute of the first object according to the first request, and generates first updated data. The first attribute is a predefined attribute. A second function obtains the first updated data and broadcasts it to multiple processes to synchronize the updates. This makes the attribute definition clearer, more intuitive, and inheritable, making the solution applicable to more application scenarios, increasing reusability and scalability, reducing the transmission of redundant data, and improving attribute synchronization efficiency. Furthermore, it eliminates the need to maintain additional synchronization code, improving development efficiency. This solves the technical problems in related technologies where server lag, code complexity, poor code reusability, and difficulty in maintaining multiple copies of code are common during cross-process data synchronization.
[0168] From the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. Therefore, the technical solutions according to the embodiments of the present invention can be embodied in the form of a software product, which can be stored in a computer-readable storage medium (such as a CD-ROM, USB flash drive, portable hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, terminal device, or network device, etc.) to execute the method according to the embodiments of the present invention.
[0169] In exemplary embodiments of this application, a computer-readable storage medium stores a program product capable of implementing the methods described above in this embodiment. In some possible implementations, various aspects of the embodiments of the present invention can also be implemented as a program product comprising program code, which, when run on a terminal device, causes the terminal device to perform the steps described in the "Exemplary Methods" section of this embodiment according to various exemplary embodiments of the present invention.
[0170] According to embodiments of the present invention, a program product for implementing the above-described method may employ a portable compact disc read-only memory (CD-ROM) and include program code, and may run on a terminal device, such as a personal computer. However, the program product of the present invention is not limited thereto. In the embodiments of the present invention, the computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in conjunction with an instruction execution system, apparatus, or device.
[0171] The aforementioned program product may take the form of any combination of one or more computer-readable media. Such computer-readable storage media may be, for example, but not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatuses, or devices, or any combination thereof. More specific examples (not exhaustive) of computer-readable storage media include: electrical connections having one or more wires, portable disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0172] It should be noted that the program code contained on the computer-readable storage medium can be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, radio frequency (RF), or any suitable combination thereof.
[0173] Embodiments of the present invention also provide an electronic device including a memory and a processor, the memory storing a computer program and the processor being configured to run the computer program to perform the steps in any of the above method embodiments.
[0174] Optionally, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0175] Optionally, in this embodiment, the processor can be configured to perform the following steps via a computer program: receiving a first request via a first function; updating a first attribute of a first object according to the first request to generate first updated data; obtaining the first updated data via a second function; and broadcasting the first updated data to multiple processes so that the multiple processes synchronously update the first updated data.
[0176] Optionally, the processor described above can also be configured to perform the following steps via a computer program: obtaining the filename attribute of the first class; binding the second class to the filename attribute of the first class; and creating the third class according to the first constructor and the second constructor.
[0177] Optionally, the processor described above can also be configured to perform the following steps via a computer program: calculate the method parsing order in the third constructor to obtain the target frame; and bind the second class to the filename attribute of the first class based on the target frame.
[0178] Optionally, the processor described above can also be configured to perform the following steps via a computer program: calling the first constructor; calling the second constructor to create a third class and multiple objects of the third class.
[0179] Optionally, the processor may also be configured to perform the following steps via a computer program: in response to the absence of a first object, create a first object and update the first data of a first attribute to second data; in response to the existence of a first object, read the first data of the first attribute and update the first data of the first attribute to second data.
[0180] Optionally, the processor described above may also be configured to perform the following steps via a computer program: storing the third data in the first dictionary of the fourth class.
[0181] Optionally, the processor described above can also be configured to perform the following steps via a computer program: create a second dictionary according to a second function; traverse the third data in the first dictionary; in response to the first attribute being a numeric attribute, save the third data to the second dictionary; in response to the first attribute being a dictionary attribute or a nested attribute, call the third function of the fourth class and the fourth function of the calling instance to change the first attribute to a numeric attribute, and save the data corresponding to the changed numeric attribute to the second dictionary; retrieve the data saved in the second dictionary to obtain the first updated data.
[0182] Optionally, the processor described above can also be configured to perform the following steps via a computer program: creating a fifth class that inherits from the fourth class, and obtaining the first updated data by calling a second function through the fifth class.
[0183] Optionally, the processor described above can also be configured to perform the following steps via a computer program: creating a sixth class that inherits from the fifth class, and obtaining the first updated data by calling a second function through the sixth class.
[0184] In the electronic device of this embodiment, a technical solution for attribute synchronization is provided. A first function receives a first request to update a first attribute of a first object, updates the first attribute of the first object according to the first request, and generates first updated data. The first attribute is a predefined attribute. A second function obtains the first updated data and broadcasts it to multiple processes so that the multiple processes can synchronously update the first updated data. This makes the attribute definition clearer, more intuitive, and inheritable, making the solution applicable to more application scenarios, increasing reusability and scalability, reducing the transmission of redundant data, and improving attribute synchronization efficiency. Furthermore, it eliminates the need to maintain additional synchronization code, improving development efficiency. This solves the technical problems in related technologies where server lag, code complexity, poor code reusability, and difficulty in maintaining multiple copies of code are common when synchronizing data across processes.
[0185] Figure 6 This is a schematic diagram of an electronic device according to an embodiment of the present invention. Figure 6 As shown, the electronic device 600 is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of the present invention.
[0186] like Figure 6 As shown, the electronic device 600 is presented in the form of a general-purpose computing device. The components of the electronic device 600 may include, but are not limited to: at least one processor 610, at least one memory 620, a bus 630 connecting different system components (including memory 620 and processor 610), and a display 640.
[0187] The memory 620 stores program code that can be executed by the processor 610, causing the processor 610 to perform the steps described in the method section of the embodiments of this application according to various exemplary implementations of the present invention.
[0188] The memory 620 may include a readable medium in the form of volatile memory cells, such as random access memory (RAM) 6201 and / or cache memory 6202, and may further include a read-only memory (ROM) 6203, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory.
[0189] In some instances, memory 620 may also include a program / utility 6204 having a set (at least one) of program modules 6205, including but not limited to: an operating system, one or more application programs, other program modules, and program data. Each or some combination of these examples may include an implementation of a network environment. Memory 620 may further include memory remotely located relative to processor 610, which can be connected to electronic device 600 via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.
[0190] Bus 630 can represent one or more of several types of bus structures, including a memory cell bus or memory cell controller, peripheral bus, graphics acceleration port, processor 610, or a local bus using any of the various bus structures.
[0191] The display 640 may be, for example, a touchscreen liquid crystal display (LCD) that allows a user to interact with the user interface of the electronic device 600.
[0192] Optionally, the electronic device 600 can also communicate with one or more external devices 600 (e.g., keyboard, pointing device, Bluetooth device, etc.), one or more devices that enable a user to interact with the electronic device 600, and / or any device that enables the electronic device 600 to communicate with one or more other computing devices (e.g., router, modem, etc.). This communication can be performed via the input / output (I / O) interface 650. Furthermore, the electronic device 600 can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN), and / or public networks, such as the Internet) via a network adapter 660. Figure 6 As shown, network adapter 660 communicates with other modules of electronic device 600 via bus 630. It should be understood that, although... Figure 6 As not shown in the diagram, other hardware and / or software modules may be used in conjunction with electronic device 600, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0193] The aforementioned electronic device 600 may further include: a keyboard, a cursor control device (such as a mouse), an input / output interface (I / O interface), a network interface, a power supply, and / or a camera.
[0194] Those skilled in the art will understand that Figure 6 The structure shown is for illustrative purposes only and does not limit the structure of the electronic device described above. For example, electronic device 600 may also include components that are more... Figure 6 The more or fewer components shown, or having the same Figure 1 Different configurations are shown. The memory 620 can be used to store computer programs and corresponding data, such as the computer program and corresponding data corresponding to the attribute synchronization method in this embodiment of the invention. The processor 610 executes various functional applications and data processing by running the computer program stored in the memory 620, thereby implementing the aforementioned attribute synchronization method.
[0195] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0196] In the above embodiments of the present invention, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0197] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units can be a logical functional division, and in actual implementation, there may be other division methods. For instance, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual coupling, direct coupling, or communication connection may be through some interfaces; the indirect coupling or communication connection between units or modules may be electrical or other forms.
[0198] The units described as separate components may or may not be physically separate. 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 units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0199] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0200] If the integrated unit is implemented as 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 invention, in essence, 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. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.
[0201] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. An attribute synchronization method, characterized in that, include: A first request is received through a first function, wherein the first function is used to receive requests from multiple processes, and the first request is used to update a first attribute of a first object; Update the first attribute of the first object according to the first request, and generate first updated data, wherein the first attribute is a predefined attribute, and the first updated data is used to reflect the data change of the first attribute; The first updated data is obtained through a second function, wherein the second function is used to obtain the updated data of the first attribute; The first update data is broadcast to the plurality of processes so that the plurality of processes synchronously update the first update data; Before receiving the first request through the first function, the method further includes: obtaining the filename attribute of a first class, wherein the first class is used to create the first object; and binding a second class to the filename attribute of the first class, wherein the second class is used to listen for changes in the first attribute. The step of binding the second class to the filename attribute of the first class includes: calculating the method parsing order in the third constructor to obtain the target frame, wherein the third constructor is used to create the first class, and the target frame is the frame instantiated from the second class; and binding the second class to the filename attribute of the first class according to the target frame.
2. The method according to claim 1, characterized in that, Before receiving the first request through the first function, it also includes: A third class is created based on the first constructor and the second constructor, wherein the first constructor is used to create the second class, the second constructor is used to create the third class, and the third class is used to store the attribute value of the first attribute.
3. The method according to claim 2, characterized in that, The creation of the third class based on the first constructor and the second constructor includes: Invoke the first constructor, wherein the first constructor includes a filename variable; The second constructor is invoked to create the third class and multiple objects of the third class. The second constructor includes the filename variable and the default variable. The multiple objects store the filename variable and the default variable. The default variable is a number or a class.
4. The method according to any one of claims 2-3, characterized in that, The step of updating the first attribute of the first object according to the first request includes: In response to the absence of the first object, the first object is created, and the first data of the first attribute is updated to the second data; In response to the existence of the first object, the first data of the first attribute is read, and the first data of the first attribute is updated to the second data.
5. The method according to claim 4, characterized in that, Also includes: The third data is stored in the first dictionary of the fourth category, wherein the third data is the difference data between the first data and the second data, and the fourth category is the base class of the first category.
6. The method according to claim 5, characterized in that, The step of obtaining the first updated data through the second function includes: Create a second dictionary based on the second function, where the second dictionary is an empty dictionary; Iterate through the third data in the first dictionary; In response to the first attribute being a numerical attribute, the third data is saved to the second dictionary; In response to the first attribute being a dictionary attribute or a nested attribute, the third function of the fourth class and the fourth function of the calling instance are called to change the first attribute to the numeric attribute, and the data corresponding to the changed numeric attribute is saved to the second dictionary; Retrieve the data stored in the second dictionary to obtain the first updated data.
7. The method according to claim 6, characterized in that, Also includes: A fifth class is created that inherits from the fourth class. The second function is called through the fifth class to obtain the first updated data, wherein the first attribute is the nested attribute.
8. The method according to claim 7, characterized in that, Also includes: A sixth class is created that inherits from the fifth class. The second function is called through the sixth class to obtain the first updated data, wherein the first attribute is the dictionary attribute.
9. An attribute synchronization device, characterized in that, include: A receiving module is configured to receive a first request through a first function, wherein the first function is configured to receive requests from multiple processes, and the first request is configured to update a first attribute of a first object; The processing module is configured to update the first attribute of the first object according to the first request and generate first updated data, wherein the first attribute is a predefined attribute and the first updated data is used to reflect the data change of the first attribute; The acquisition module is used to acquire the first updated data through a second function, wherein the second function is used to acquire the updated data of the first attribute; The broadcast module is used to broadcast the first updated data to the plurality of processes so that the plurality of processes synchronously update the first updated data; The processing module is further configured to obtain the filename attribute of a first type, wherein the first type is used to create the first object; and to bind a second type to the filename attribute of the first type, wherein the second type is used to listen for changes in the first attribute. The processing module is further configured to calculate the method parsing order in the third constructor to obtain the target frame, wherein the third constructor is configured to create the first class, and the target frame is a frame instantiated from the second class; and to bind the second class to the filename attribute of the first class according to the target frame.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program is configured to execute the attribute synchronization method as described in any one of claims 1 to 8 when run on a computer or processor.
11. An electronic device comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to run the computer program to perform the attribute synchronization method as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Data synchronization method and device
CN109603149A