CN117270831B - Protocol class synchronization and cooperative program call compatible implementation method - Google Patents

Protocol class synchronization and cooperative program call compatible implementation method Download PDF

Info

Publication number
CN117270831B
CN117270831B CN202311532331.5A CN202311532331A CN117270831B CN 117270831 B CN117270831 B CN 117270831B CN 202311532331 A CN202311532331 A CN 202311532331A CN 117270831 B CN117270831 B CN 117270831B
Authority
CN
China
Prior art keywords
protocol
class
function
asynchronous
processor
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202311532331.5A
Other languages
Chinese (zh)
Other versions
CN117270831A (en
Inventor
庞志斌
刘斌
孙昊鹏
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Tianjin Hualai Technology Co Ltd
Original Assignee
Tianjin Hualai Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Tianjin Hualai Technology Co Ltd filed Critical Tianjin Hualai Technology Co Ltd
Priority to CN202311532331.5A priority Critical patent/CN117270831B/en
Publication of CN117270831A publication Critical patent/CN117270831A/en
Application granted granted Critical
Publication of CN117270831B publication Critical patent/CN117270831B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/315Object-oriented languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/33Intelligent editors
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/02Protocols based on web technology, e.g. hypertext transfer protocol [HTTP]

Abstract

The invention provides a protocol class synchronization and cooperative program call compatible realization method, which comprises the following steps: defining a protocol test class to receive an instance object, converting each synchronous protocol function into a coroutine function and binding the coroutine function to the instance; defining a protocol action class as a decorator class, instantiating a protocol test class in the protocol action class, and defining an asynchronous judgment variable with an initial value of 0; creating an asynchronous method function in the protocol action class; creating an attribute acquisition function in the protocol action class, wherein the attribute acquisition function carries out different treatments according to the asynchronous judgment variable; defining a protocol class and creating an initialization function, initializing a decorator class when the protocol class is instantiated. The invention has the beneficial effects that: the developer can call the synchronous protocol class under the synchronous environment and the asynchronous coroutine environment only by normally writing the synchronous protocol class.

Description

Protocol class synchronization and cooperative program call compatible implementation method
Technical Field
The invention belongs to the technical field of computers, and particularly relates to a protocol class synchronization and cooperative call compatible implementation method.
Background
As the complexity of modern software systems increases, API (application program interface) calls become a key component of system internal and system-to-system interactions. The HTTP protocol is one of the main protocols used in modern web applications, and is widely used for API calls due to its reliability and efficiency.
The python language is currently widely used in the industry for automated testing of http interfaces. Most of the framework principles widely used by open sources are to write interface protocol class codes according to an http protocol, and then add a decorator to the interface protocol class to process various services.
However, the protocol classes written by the method are synchronous writing methods, when some concurrent scene tests are carried out or some third-party protocol asynchronous frames are accessed, the original protocol classes cannot be directly called, and a set of protocol classes supporting protocol calling needs to be additionally written. The process is complicated, and most of time is in the migration of the interface protocol, and two sets of systems are required to be maintained for the subsequent protocol addition and modification, so that the development cost is greatly increased, and the development quality and efficiency are reduced.
In summary, how to realize synchronous call and asynchronous cooperative call of python protocol class through a set of system becomes a business technical problem to be solved.
Disclosure of Invention
In view of the foregoing, the present invention aims to provide a method for implementing protocol class synchronization and cooperative call compatibility, so as to solve at least one of the above technical problems.
In order to achieve the above purpose, the technical scheme of the invention is realized as follows:
the invention provides a protocol class synchronization and cooperative program call compatible implementation method, which comprises the following steps:
s1, defining a protocol test class to receive an instance object, converting each synchronous protocol function into a coroutine function and binding the coroutine function to the instance;
s2, defining a protocol action class as a decorator class, instantiating a protocol test class in the protocol action class, and defining an asynchronous judgment variable with an initial value of 0;
s3, creating an asynchronous method function in the protocol action class, setting an asynchronous judgment variable to be 1 in the calling process of the asynchronous method function, and returning to the self object;
s4, creating an attribute acquisition function in the protocol action class, wherein the attribute acquisition function carries out different treatments according to the asynchronous judgment variable;
a protocol class is defined and an initialization function is created, initializing the decorator class upon instantiation of the protocol class.
Further, the specific process of step S1 is as follows:
the initialization function of the protocol test class receives obj parameters, and the obj parameters are transferred into an instance object of the protocol class when being called;
defining an initialization method, wherein the initialization method obtains protocol functions of an instance object through a directory method, and carries out coroutine transformation on each protocol function through for-loop traversal to obtain coroutine functions;
binding the coroutine function to the protocol class instance by a setattr method, and adding an asynchronous key word for the coroutine function.
Further, the specific process of step S2 is as follows:
creating an initialization function in the protocol test class, and instantiating the protocol test class in the initialization function;
the initialization method for calling the protocol test class wraps functions in the protocol class into asynchronous functions which can be called in a cooperative way;
an asynchronous judgment variable is defined to judge whether an external calling party calls a synchronous protocol function or an asynchronous protocol function, and the initial value of the asynchronous judgment variable is set to 0.
Further, the specific process of step S4 is as follows:
when the protocol class function is called externally, the attribute acquisition function is automatically called, the attribute acquisition function judges whether the value of the asynchronous judgment variable is 1, if the value is 1, the asynchronous function wrapped to be the cooperative call is returned, the value of the asynchronous judgment variable is set to be 0, and if the value is 0, the original protocol function is directly returned.
In a second aspect the invention provides an electronic device comprising a processor and a memory communicatively coupled to the processor for storing instructions executable by the processor, characterized in that: the processor is configured to execute a protocol class synchronization and coordination call compatible implementation method according to any one of the first aspect.
In a third aspect, the present invention provides a server, characterized in that: the system comprises at least one processor and a memory communicatively connected with the processor, wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the processor to cause the at least one processor to execute a protocol class synchronization and coroutine compatible implementation method according to any one of the first aspect.
A fourth aspect of the present invention provides a computer-readable storage medium storing a computer program, characterized in that: the computer program, when executed by a processor, implements a protocol class synchronization and coroutine call compatible implementation method according to any one of the first aspects.
Compared with the prior art, the protocol class synchronization and cooperative call compatible implementation method has the following beneficial effects:
1) By using the technical scheme of the invention, a developer can call in a synchronous environment and an asynchronous coroutine environment at the same time by only normally writing a set of synchronous protocol classes.
2) The logic codes of the scheme are processed in the decorator class and isolated from the protocol class codes, and unnecessary attention is not needed when a plurality of protocol classes are compiled, so long as the protocol classes are ensured to inherit the decorator class.
3) The scheme is simple in synchronous and asynchronous switching and calling, supports a chained calling mode, and can normally identify and give out a protocol function prompt when the cooperative program asynchronous calling is written, so that a developer can work conveniently.
Drawings
The accompanying drawings, which are included to provide a further understanding of the invention and are incorporated in and constitute a part of this specification, illustrate embodiments of the invention and together with the description serve to explain the invention. In the drawings:
fig. 1 is a schematic flow chart of a protocol class synchronization and cooperative call compatible implementation method according to an embodiment of the present invention.
Detailed Description
It should be noted that, without conflict, the embodiments of the present invention and features of the embodiments may be combined with each other.
The invention will be described in detail below with reference to the drawings in connection with embodiments.
Embodiment one:
a protocol class synchronization and cooperative program call compatible implementation method comprises the following steps:
s1, defining an AbcAsyncProtocolTest class (protocol test class) for realizing the function of converting a synchronous protocol function into a cooperative call function;
the __ init __ function of the abcasyncptocol test class accepts obj parameters that are transferred into the instance object of the protocol class when called;
defining init () function, obtaining all protocol functions of the instance object of the protocol class through dir () method in the function, performing coroutization transformation on each protocol function through for circulation traversal, binding the protocol function after coroutization to the protocol class instance through setattr method, and naming the format as async_xxx (xxx is the original name of the protocol function).
S2, defining a protocol class and a decorator class protocol action, wherein the protocol class inherits from the decorator class;
__ init __ initialization functions are realized in the protocol action, abcAsyncProtocolTes class is instantiated in the functions, and an init method of the AbcAsyncProtocolTes class is called to wrap all functions of the protocol class into asynchronous functions which can be called by a cooperative program;
the definition is_async variable is used to determine whether an external caller calls a synchronous protocol function or an asynchronous protocol function, and the variable initial value is 0.
S3, an async_func function is implemented in a protocol action class, and the function sets the value of an is_async variable to be 1 when in call, and returns self objects for facilitating chained call.
S4, realizing a __ getttribute __ function in a protocol action class;
when the protocol class function is called externally, the function is called automatically, and whether the value of the instance variable is_async is 1 is added in the function;
if 1, the function of the protocol Cheng Baoguo (i.e., the async_xxx function mentioned above) is returned and the value of is_async is set to 0;
if the value of is_async is 0, the original protocol function is directly returned.
__ init __ initialization functions are implemented in custom protocol classes;
the super (). __ init __ () method is added at the end of this function to initialize the class decorator mentioned above at the time of protocol class instantiation.
Embodiment two:
an electronic device comprising a processor and a memory communicatively coupled to the processor for storing processor-executable instructions, characterized in that: the processor is configured to execute a protocol class synchronization and coordination call compatible implementation method according to any one of the foregoing embodiments.
Embodiment III:
a server, characterized by: the system comprises at least one processor and a memory communicatively connected with the processor, wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the processor to cause the at least one processor to execute a protocol class synchronization and coroutine compatible implementation method according to any one of the embodiments.
Embodiment four:
a computer-readable storage medium storing a computer program, characterized in that: the method for implementing protocol class synchronization and cooperative call compatibility according to any one of the embodiments is implemented when the computer program is executed by a processor.
Those of ordinary skill in the art will appreciate that the elements and method steps of each example described in connection with the embodiments disclosed herein may be implemented as electronic hardware, computer software, or combinations of both, and that the elements and steps of each example have been described generally in terms of functionality in the foregoing description to clearly illustrate this interchangeability of hardware and software. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the solution. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present invention.
In the several embodiments provided in this application, it should be understood that the disclosed methods and systems may be implemented in other ways. For example, the above-described division of units is merely a logical function division, and there may be another division manner when actually implemented, for example, a plurality of units or components may be combined or may be integrated into another system, or some features may be omitted or not performed. The units may or may not be physically separate, and components shown as units may or may not be physical units, may be located in one place, or may be distributed over a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiment of the present invention.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present invention, and not for limiting the same; although the invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some or all of the technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit of the invention, and are intended to be included within the scope of the appended claims and description.
The foregoing description of the preferred embodiments of the invention is not intended to be limiting, but rather is intended to cover all modifications, equivalents, alternatives, and improvements that fall within the spirit and scope of the invention.

Claims (4)

1. A protocol class synchronization and cooperative program call compatible implementation method is characterized in that:
the method comprises the following steps:
s1, defining a protocol test class to receive an instance object, converting each synchronous protocol function into a coroutine function and binding the coroutine function to the instance;
s2, defining a protocol action class as a decorator class, instantiating a protocol test class in the protocol action class, and defining an asynchronous judgment variable with an initial value of 0;
s3, creating an asynchronous method function in the protocol action class, setting an asynchronous judgment variable to be 1 in the calling process of the asynchronous method function, and returning to the self object;
s4, creating an attribute acquisition function in the protocol action class, wherein the attribute acquisition function carries out different treatments according to the asynchronous judgment variable;
defining a protocol class and creating an initialization function, and initializing a decorator class when the protocol class is instantiated;
the specific process of step S1 is as follows:
the initialization function of the protocol test class receives obj parameters, and the obj parameters are transferred into an instance object of the protocol class when being called;
defining an initialization method, wherein the initialization method obtains protocol functions of an instance object through a directory method, and traverses each protocol function to carry out protocol transformation on the protocol functions to obtain a protocol function;
binding the coroutine function to a protocol class instance through a setattr method, and adding an asynchronous keyword for the coroutine function;
the specific process of step S2 is as follows:
implementing an initialization function in the protocol test class, and instantiating the protocol test class in the initialization function;
the initialization method for calling the protocol test class wraps functions in the protocol class into asynchronous functions which can be called in a cooperative way;
defining an asynchronous judgment variable to judge whether an external calling party calls a synchronous protocol function or an asynchronous protocol function, and setting the initial value of the asynchronous judgment variable to be 0;
the specific process of step S4 is as follows:
when the protocol class function is called externally, the attribute acquisition function is automatically called, the attribute acquisition function judges whether the value of the asynchronous judgment variable is 1, if the value is 1, the asynchronous function wrapped to be the cooperative call is returned, the value of the asynchronous judgment variable is set to be 0, and if the value is 0, the original protocol function is directly returned.
2. An electronic device comprising a processor and a memory communicatively coupled to the processor for storing processor-executable instructions, characterized in that: the processor is configured to execute a protocol class synchronization and coordination call compatible implementation method as set forth in claim 1.
3. A server, characterized by: comprising at least one processor and a memory communicatively coupled to the processor, the memory storing instructions executable by the at least one processor to cause the at least one processor to perform a protocol class synchronization and coroutine compatibility implementation as recited in claim 1.
4. A computer-readable storage medium storing a computer program, characterized in that: the computer program, when executed by a processor, implements a protocol class synchronization and coroutine call compatible implementation method as defined in claim 1.
CN202311532331.5A 2023-11-17 2023-11-17 Protocol class synchronization and cooperative program call compatible implementation method Active CN117270831B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311532331.5A CN117270831B (en) 2023-11-17 2023-11-17 Protocol class synchronization and cooperative program call compatible implementation method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311532331.5A CN117270831B (en) 2023-11-17 2023-11-17 Protocol class synchronization and cooperative program call compatible implementation method

Publications (2)

Publication Number Publication Date
CN117270831A CN117270831A (en) 2023-12-22
CN117270831B true CN117270831B (en) 2024-02-23

Family

ID=89204768

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311532331.5A Active CN117270831B (en) 2023-11-17 2023-11-17 Protocol class synchronization and cooperative program call compatible implementation method

Country Status (1)

Country Link
CN (1) CN117270831B (en)

Citations (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7248603B1 (en) * 2001-06-28 2007-07-24 Microsoft Corporation Asynchronous pattern
CN101965561A (en) * 2008-03-04 2011-02-02 微软公司 Declarative support for asynchronous methods
WO2016192556A1 (en) * 2015-05-29 2016-12-08 北京金山安全软件有限公司 Interface invoking method, device and terminal
CN107992344A (en) * 2016-10-25 2018-05-04 腾讯科技(深圳)有限公司 One kind association's journey implementation method and device
CN111767159A (en) * 2020-06-24 2020-10-13 浙江大学 Asynchronous system calling system based on coroutine
CN112286595A (en) * 2020-10-30 2021-01-29 深圳市九九互动科技有限公司 Asynchronous processing method and device of Lua language, computer equipment and storage medium
KR102262093B1 (en) * 2020-07-22 2021-06-08 한국과학기술정보연구원 Gateway apparatus, and control method thereof
CN114584500A (en) * 2022-02-25 2022-06-03 网易(杭州)网络有限公司 Asynchronous communication testing method and device and electronic equipment
CN115297039A (en) * 2022-10-08 2022-11-04 天津华来科技股份有限公司 HTTP (hyper text transport protocol) parameterized test architecture method, electronic equipment and storage medium
CN115390920A (en) * 2022-09-20 2022-11-25 平安国际融资租赁有限公司 Event stream processing method and device, computer equipment and storage medium
CN115599287A (en) * 2021-06-28 2023-01-13 深信服科技股份有限公司(Cn) Data processing method and device, electronic equipment and storage medium
CN115640146A (en) * 2022-09-30 2023-01-24 拉卡拉支付股份有限公司 Back-end service calling method and device, electronic equipment and program product
CN116009995A (en) * 2022-12-27 2023-04-25 网易(杭州)网络有限公司 Asynchronous coroutine implementation method and device and electronic equipment

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7739656B2 (en) * 2004-12-15 2010-06-15 International Business Machines Corporation Generating asynchronous interfaces and methods from synchronous interfaces and methods

Patent Citations (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7248603B1 (en) * 2001-06-28 2007-07-24 Microsoft Corporation Asynchronous pattern
CN101965561A (en) * 2008-03-04 2011-02-02 微软公司 Declarative support for asynchronous methods
WO2016192556A1 (en) * 2015-05-29 2016-12-08 北京金山安全软件有限公司 Interface invoking method, device and terminal
CN107992344A (en) * 2016-10-25 2018-05-04 腾讯科技(深圳)有限公司 One kind association's journey implementation method and device
CN111767159A (en) * 2020-06-24 2020-10-13 浙江大学 Asynchronous system calling system based on coroutine
KR102262093B1 (en) * 2020-07-22 2021-06-08 한국과학기술정보연구원 Gateway apparatus, and control method thereof
CN112286595A (en) * 2020-10-30 2021-01-29 深圳市九九互动科技有限公司 Asynchronous processing method and device of Lua language, computer equipment and storage medium
CN115599287A (en) * 2021-06-28 2023-01-13 深信服科技股份有限公司(Cn) Data processing method and device, electronic equipment and storage medium
CN114584500A (en) * 2022-02-25 2022-06-03 网易(杭州)网络有限公司 Asynchronous communication testing method and device and electronic equipment
CN115390920A (en) * 2022-09-20 2022-11-25 平安国际融资租赁有限公司 Event stream processing method and device, computer equipment and storage medium
CN115640146A (en) * 2022-09-30 2023-01-24 拉卡拉支付股份有限公司 Back-end service calling method and device, electronic equipment and program product
CN115297039A (en) * 2022-10-08 2022-11-04 天津华来科技股份有限公司 HTTP (hyper text transport protocol) parameterized test architecture method, electronic equipment and storage medium
CN116009995A (en) * 2022-12-27 2023-04-25 网易(杭州)网络有限公司 Asynchronous coroutine implementation method and device and electronic equipment

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
Automatic migration from synchronous to asynchronous JavaScript APIs;Satyajit Gokhale;《Proceedings of the ACM on Programming Languages》;20211015;第5卷;全文 *
黄永祥.《精通Python自动化编程》.机械工业出版社,2021,311-313. *

Also Published As

Publication number Publication date
CN117270831A (en) 2023-12-22

Similar Documents

Publication Publication Date Title
US11010283B2 (en) Mock-based unit test(s) for an end-to-end test of a code snippet
CN105913316B (en) Rule configuration method and device
CN104714828A (en) Methods and devices for installing and operating application
CN111597110B (en) Page test method and device, electronic equipment and storage medium
KR20050076719A (en) System and method for dynamically adding features to software applications
CN111176791B (en) Cross-virtual machine calling method based on multi-virtual machine blockchain platform
WO2019090994A1 (en) Script testing automated execution method, apparatus, equipment and storage medium
CN108011768A (en) A kind of aop intercept types HBase data store micro services framework construction method
WO2019238563A1 (en) System and methods with reduced complexity in the integration of exposed information models with applications
US20210224055A1 (en) Data interface processing method, device, server and medium
US6799320B1 (en) Providing binding options for component interfaces
CN111262753B (en) Method, system, terminal and storage medium for automatically configuring number of NUMA nodes
US20100162275A1 (en) Controlling applications through inter-process communication
CN111506368A (en) Method, device, equipment and storage medium for transferring asynchronous call to synchronous call
CN117270831B (en) Protocol class synchronization and cooperative program call compatible implementation method
CN116521181B (en) Script data processing method, device, equipment and medium based on game system
CN112835615A (en) Plug-in processing method and device for software development kit and electronic equipment
CN117234582A (en) Project code processing method and device, electronic equipment and storage medium
CN110599112A (en) Method and device for developing and maintaining network page
CN113779638A (en) Method and device for realizing interface caller authentication in dynamic proxy mode
CN108733401B (en) Method and device for realizing browser compatibility
CN113312031A (en) Naming service interface of software communication system structure
CN108959061B (en) Application function management method, terminal and device
US20040216140A1 (en) Method and system for accessing system operations through an interface layer
CN110399161A (en) A kind of generation method of mapping relations, call method and device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant