WO2019047376A1 - 接口任务调用方法、装置、电子设备及可读存储介质 - Google Patents

接口任务调用方法、装置、电子设备及可读存储介质 Download PDF

Info

Publication number
WO2019047376A1
WO2019047376A1 PCT/CN2017/111927 CN2017111927W WO2019047376A1 WO 2019047376 A1 WO2019047376 A1 WO 2019047376A1 CN 2017111927 W CN2017111927 W CN 2017111927W WO 2019047376 A1 WO2019047376 A1 WO 2019047376A1
Authority
WO
WIPO (PCT)
Prior art keywords
task
callback
queue
processing
level
Prior art date
Application number
PCT/CN2017/111927
Other languages
English (en)
French (fr)
Inventor
黄浩
张文明
陈少杰
Original Assignee
武汉斗鱼网络科技有限公司
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 武汉斗鱼网络科技有限公司 filed Critical 武汉斗鱼网络科技有限公司
Publication of WO2019047376A1 publication Critical patent/WO2019047376A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • G06F9/4843Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
    • G06F9/4881Scheduling strategies for dispatcher, e.g. round robin, multi-level priority queues

Definitions

  • the present invention relates to the field of data processing technologies, and in particular, to an interface task calling method, apparatus, electronic device, and readable storage medium.
  • some common methods are to add a mutex lock on the interface for writing and reading data memory, that is, blocking the target data interface of other threads to the data memory before the target data interface of one thread accessing the data memory is completed. Access.
  • the location of the mutex is increased, which causes the interface access speed to decrease, which affects concurrency, and may even lead to deadlocks and program suspended animations.
  • the contention access will bring additional resource overhead, reduce the response speed of the thread, affect the thread running by the program, frequent thread blocking and wake-up actions will also increase the CPU work. burden.
  • one of the objects of the present invention is to provide an interface task calling method, which is applied to an electronic device, and the method includes:
  • Another object of the present invention is to provide an interface task calling device, which is applied to an electronic device, and the device includes:
  • setup module configured to establish a task queue and a loop processing function configured to process tasks in the task queue
  • the receiving module is configured to receive a callback task delivered by the worker thread and put it into the task queue, where the callback task is encapsulated by a target data interface that the worker thread needs to invoke, and the callback task includes a corresponding priority grade;
  • the task processing module is configured to batch extract and store the callback task in the task queue using the loop processing function, and sequentially process the callback task according to a priority level of the callback task.
  • the above-described interface task installation device installed/stored in the memory and executed by the processor.
  • the present invention has the following beneficial effects:
  • the invention provides an interface task calling method, device, electronic device and readable storage medium.
  • the worker thread delivers a corresponding callback to the task queue when the target data interface needs to be called.
  • the task then the loop handler, processes the postback callback tasks in turn.
  • the method provided in this embodiment can implement the thread accessing the target data interface one by one at the fastest speed, and there is no unnecessary locking operation, which saves system running resources and improves the processing capability of multi-thread concurrent processing.
  • FIG. 1 is a schematic diagram of an electronic device according to an embodiment of the present invention.
  • FIG. 2 is a schematic flowchart of steps of an interface task invoking method according to an embodiment of the present invention
  • FIG. 3 is a second schematic flowchart of steps of an interface task calling method according to an embodiment of the present disclosure
  • FIG. 4 is a schematic diagram of an interface task invoking device according to an embodiment of the present invention.
  • Icons 100-electronic device; 110-interface task invoking device; 111-building module; 112-receiving module; 113-task processing module; 120-memory; 130-processor.
  • FIG. 1 is a schematic diagram of an electronic device 100 according to an embodiment.
  • the electronic device 100 may be, but not limited to, a server, a smart phone, a personal computer (PC), Tablet PC, personal digital assistant (PDA), mobile Internet device (MID), etc.
  • PC personal computer
  • PDA personal digital assistant
  • MID mobile Internet device
  • the electronic device 100 includes an interface task invoking device 110, a memory 120, and a processor 130.
  • the memory 120 and the components of the processor 130 are electrically connected directly or indirectly to each other to implement data transmission or interaction.
  • the components can be electrically connected to one another via one or more communication buses or signal lines.
  • the interface task invoking device 110 includes at least one software function module that can be stored in the memory 120 or in an operating system (OS) of the electronic device 100 in the form of software or firmware.
  • the processor 130 is configured to execute an executable module stored in the memory 120, such as a software function module, a computer program, and the like included in the interface task calling device 110.
  • the memory 120 may be, but not limited to, a random access memory (RAM), a read only memory (ROM), and a programmable read-only memory (PROM). Erasable Programmable Read-Only Memory (EPROM), Electric Erasable Programmable Read-Only Memory (EEPROM), and the like.
  • RAM random access memory
  • ROM read only memory
  • PROM programmable read-only memory
  • EPROM Erasable Programmable Read-Only Memory
  • EEPROM Electric Erasable Programmable Read-Only Memory
  • the memory 120 is configured to store a program, and the processor 130 executes the program after receiving an execution instruction.
  • the processor 130 may be an integrated circuit chip with signal processing capabilities.
  • the above processor may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; or may be a digital signal processor (DSP) or an application specific integrated circuit (ASIC).
  • DSP digital signal processor
  • ASIC application specific integrated circuit
  • FPGA field-programmable gate array
  • the above-described general-purpose processors may implement or perform the various methods, steps, and logic blocks disclosed in the embodiments of the present invention.
  • the general purpose processor may be a microprocessor or the processor or any conventional processor or the like.
  • the target data interface to be accessed is encapsulated as a callback task, that is, the worker thread delivers a corresponding callback task when the target data interface needs to be accessed, and the electronic device 100 sequentially processes the abstracted callback task. Callback task.
  • the control of accessing the target data interface can be implemented without locking the worker thread, so that only one worker thread accesses the target data interface at the same time.
  • FIG. 2 is a flowchart of an interface task calling method applied to the electronic device 100 shown in FIG. 1. The method includes the following steps.
  • Step S110 establishing a task queue and a loop processing function for processing tasks in the task queue.
  • a task receiving object for example, an Incoming Task Queue object
  • the task receiving task internally creates a task queue of std::queue ⁇ PendingTask>.
  • This task queue is used to save callback tasks that are delivered from the worker thread. Because the std::queue structure has the advantage of accessing data from the head and tail of the queue more quickly, it can be queued in time when receiving a large number of postback callback tasks.
  • a message loop MessageLoop object which is the actual body that the worker thread runs.
  • the message loop is used to obtain the callback task from the IncomingTaskQueue object and execute it. Each time the task saved in MessageLoop is executed first, so that the task loop operation speed can be improved without frequently obtaining the task from the IncomingTaskQueue every time.
  • threadProc is the working process interface of the worker thread. Then run the MessageLoop Run function inside threadProc, open the message loop of the worker thread, and wait for the callback task.
  • Step S120 Receive a callback task delivered by the worker thread and put it into the task queue, where the callback task is encapsulated by a target data interface that needs to be called by the worker thread, and the callback task includes a corresponding priority level.
  • the worker thread delivers a corresponding callback task to the task queue when it needs to access the target data interface.
  • the callback task can set a corresponding priority level according to the importance.
  • the callback task may include: a first level task that is highly important and needs to be processed immediately. In this embodiment, it may be recorded as a PostTask; a second level task capable of delay processing may be recorded in this embodiment. It is PostDelayedTask; the third level task that can be processed when the worker thread is idle, in this embodiment, can be recorded as PostIdleWork.
  • Step S130 using the loop processing function to batch extract and store the callback tasks in the task queue, and sequentially processing the callback tasks according to the priority level of the callback tasks.
  • the method may further include step S140.
  • Step S140 after processing the callback task extracted by the current round, extracting a new unprocessed callback task from the task queue again. In this way, the loop extraction process of the callback task is implemented.
  • the loop processing function may batch extract the callback task from the task queue every predetermined time interval. This enables loop extraction processing of the callback task.
  • the loop processing function When processing the extracted callback task, the loop processing function first processes the extracted first level task because the first level task is more important.
  • the second level task is processed.
  • the extracted third level task is processed. That is, the loop processing function processes the second level task after detecting that the extracted callback task does not have the first level task and the second level task. Because the third-level task belongs to an idle task, it is used to process the type task when the worker thread is idle, so that the utilization of the worker thread can be improved, and the processing power of the worker thread can be utilized to the maximum extent.
  • the loop processing function extracts the callback task from the task queue, if it is determined that there is no callback task in the task queue, the preset waiting function is executed to enter the sleep state. .
  • the MessageLoop object temporarily calls the wait function, causing the worker thread to enter the background sleep state. This can reduce the performance overhead of the program running worker thread.
  • the loop processing function when the loop processing function is in a sleep state, it is detected whether a callback task is delivered to the task queue.
  • the loop processing function is awakened to extract a callback task from the task queue and process it. That is, when the worker thread has a callback task delivered, ScheduleWork will be called to wake up the MessageLoop message loop of the current worker thread, and continue to wait for the callback task to be executed.
  • the method provided by the embodiment can implement the target data interface on a thread-by-thread basis at the fastest speed. And there is no redundant locking operation, which saves the system running resources and improves the processing capability of multi-thread concurrent processing.
  • the embodiment further provides an interface task invoicing apparatus 110 applied to the electronic device 100 shown in FIG. 1.
  • the apparatus includes an establishing module 111, a receiving module 112, and a task processing module 113.
  • the establishing module 111 is configured to establish a task queue and a loop processing function for processing tasks in the task queue.
  • the establishing module 111 can be used to perform the step S110 shown in FIG. 2, and the description of the step S110 can be referred to in the specific description of the establishing module 111.
  • the receiving module 112 is configured to receive a callback task delivered by the worker thread and put it into the task queue.
  • the callback task is encapsulated by a target data interface that the worker thread needs to invoke, and the callback task includes a corresponding priority level.
  • the receiving module 112 can be used to perform step S120 shown in FIG. 2, and a detailed description of the receiving module 112 can refer to the description of the step S120.
  • the task processing module 113 is configured to batch extract and store the callback tasks in the task queue using the loop processing function, and sequentially process the callback tasks according to the priority level of the callback tasks.
  • the task processing module 113 can be used to perform step S130 shown in FIG. 2, and a detailed description of the task processing module 113 can refer to the description of the step S130.
  • the apparatus further includes a loop extraction module.
  • the loop extraction module is configured to batch-extract a new unprocessed callback task from the task queue again after processing the callback task extracted by the current round.
  • the task processing module 113 extracts and stores the callback task in the task queue by:
  • the callback task is batch extracted from the task queue every predetermined time interval.
  • the callback task includes a first level task that requires a higher degree of importance, a second level task that can be delayed, and a third level task that can be processed when the thread is idle.
  • the task processing module 113 processes the callback task in the following manner:
  • the extracted third level task is processed.
  • the task processing module is configured to: when the loop processing function detects that there is no callback task in the task queue, execute a preset wait function and enter a sleep state.
  • the task processing module is further configured to:
  • the loop processing function When it is detected that a callback task is posted to the task queue, the loop processing function is awakened to extract a callback task from the task queue and process it.
  • the present invention provides an interface task calling method and apparatus, by encapsulating a target interface to be called as a callback task, when the worker thread needs to call the target data interface, the task queue is delivered to the task queue to obtain a callback task, and then The loop processing function processes the postback callback tasks in turn.
  • the method provided in this embodiment can be The fastest speed realizes that the thread calls the target data interface one by one, and there is no redundant locking operation, which saves the system running resources and improves the processing capability of multi-thread concurrent processing.
  • the embodiment of the present invention provides a readable storage medium, on which a computer program is stored, and the computer program implements the interface task calling method provided by the foregoing embodiment when executed.
  • each block of the flowchart or block diagram can represent a module, a program segment, or a portion of code that includes one or more of the Executable instructions. It should also be noted that, in some alternative implementations, the functions noted in the blocks may also occur in a different order than those illustrated in the drawings.
  • each block of the block diagrams and/or flowcharts, and combinations of blocks in the block diagrams and/or flowcharts can be implemented in a dedicated hardware-based system that performs the specified function or action. Or it can be implemented by a combination of dedicated hardware and computer instructions.
  • each functional module in each embodiment of the present invention may be integrated to form a separate part, or each module may exist separately, or two or more modules may be integrated to form a separate part.
  • the functions, if implemented in the form of software functional modules and sold or used as separate products, may be stored in a computer readable storage medium.
  • the technical solution of the present invention which is essential or contributes to the prior art, or a part of the technical solution, may be embodied in the form of a software product, which is stored in a storage medium, including
  • the instructions are used to cause a computer device (which may be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods described in various embodiments of the present invention.
  • the foregoing storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, and the like. .
  • An interface task calling method, device, electronic device and readable storage medium provided by an embodiment of the present invention, by encapsulating a target interface to be called as a callback task, when the working thread needs to call the target data interface, delivering the corresponding task to the task queue The callback task, and then the loop handler to process the postback callback tasks in turn.
  • the method provided in this embodiment can implement the thread accessing the target data interface one by one at the fastest speed, and there is no unnecessary locking operation, which saves system running resources and improves the processing capability of multi-thread concurrent processing.

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)
  • Debugging And Monitoring (AREA)

Abstract

本发明提供一种接口任务调用方法、装置、电子设备及可读存储介质。所述方法包括:建立任务队列以及用于处理所述任务队列中任务的循环处理函数;接收工作线程投递的回调任务并放入所述任务队列中,其中,所述回调任务由该工作线程需要调用的目标数据接口封装而成,所述回调任务包括相应的优先等级;使用所述循环处理函数批量提取并存储所述任务队列中的所述回调任务,并根据所述回调任务的优先等级依次处理所述回调任务。如此,本实施例提供的方法可以以最快的速度实现线程逐个调用访问目标数据接口,并且没有多余的加锁的操作,节约了系统运行资源,提高了多线程并发的处理能力。

Description

接口任务调用方法、装置、电子设备及可读存储介质
本申请要求于2017年9月05日提交中国专利局的申请号为CN2017107890027、名称为“接口任务调用方法及装置”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。
技术领域
本发明涉及数据处理技术领域,具体而言,涉及一种接口任务调用方法、装置、电子设备及可读存储介质。
背景技术
目前,可能会有多线程同时访问一个目标数据接口的情况,若一个线程在从数据接口向数据内存中写入数据的同时,另一个线程在通过数据接口从该数据内存中读取数据,会对该数据内存造成读写冲突,导致存储的数据不完整。为解决以上问题,常用的一些做法是在写入和读取数据内存的接口增加互斥锁,即在一个线程访问数据内存的目标数据接口完成之前,阻止其他线程对该数据内存的目标数据接口的访问。但是,在线程数较多时,增加互斥锁的位置较多,造成接口访问速度降低,影响了并发性,甚至可能导致死锁、程序假死等情况。并且,多个线程同时获取一个加锁对象时,竞争访问会带来额外的资源开销,降低线程的响应速度,影响了程序运行的线程,频繁的线程阻塞和唤醒动作也会加大CPU的工作负担。
发明内容
为了克服现有技术中的上述不足,本发明的目之一的在于提供一种接口任务调用方法,应用于电子设备,所述方法包括:
建立任务队列以及用于处理所述任务队列中任务的循环处理函数;
接收工作线程投递的回调任务并放入所述任务队列中,其中,所述回调任务由该工作线程需要调用的目标数据接口封装而成,所述回调任务包括相应的优先等级;
使用所述循环处理函数批量提取并存储所述任务队列中的所述回调任务,并根据所述回调任务的优先等级依次处理所述回调任务。
本发明的另一目的在于提供一种接口任务调用装置,应用于电子设备,所述装置包括:
建立模块,被配置成建立任务队列以及被配置成处理所述任务队列中任务的循环处理函数;
接收模块,被配置成接收工作线程投递的回调任务并放入所述任务队列中,其中,所述回调任务由该工作线程需要调用的目标数据接口封装而成,所述回调任务包括相应的优先等级;
任务处理模块,被配置成使用所述循环处理函数批量提取并存储所述任务队列中的所述回调任务,并根据所述回调任务的优先等级依次处理所述回调任务。
本发明的再一目的在于提供一种电子设备,包括:
存储器;
处理器;
安装/存储于所述存储器并由所述处理器执行的上述的接口任务调用装置。
本发明的再一目的在于提供一种可读存储介质,其上存储有计算机程序,所述计算机程序在执行时实现上述的接口任务调用方法。
相对于现有技术而言,本发明具有以下有益效果:
本发明提供的一种接口任务调用方法、装置、电子设备及可读存储介质,通过将需要调用的目标接口封装为回调任务,工作线程在需要调用目标数据接口时,向任务队列投递相应的回调任务,然后循环处理函数对投递回调任务进行依次处理。如此,本实施例提供的方法可以最快的速度实现线程逐个调用访问目标数据接口,并且没有多余的加锁操作,节约了系统运行资源,提高了多线程并发的处理能力。
附图说明
为了更清楚地说明本发明实施例的技术方案,下面将对实施例中所需要使用的附图作简单地介绍,应当理解,以下附图仅示出了本发明的某些实施例,因此不应被看作是对范围的限定,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他相关的附图。
图1为本发明实施例提供的电子设备的示意图;
图2为本发明实施例提供的接口任务调用方法的步骤流程示意图之一;
图3为本发明实施例提供的接口任务调用方法的步骤流程示意图之二;
图4为本发明实施例提供的接口任务调用装置的示意图。
图标:100-电子设备;110-接口任务调用装置;111-建立模块;112-接收模块;113-任务处理模块;120-存储器;130-处理器。
具体实施方式
为使本发明实施例的目的、技术方案和优点更加清楚,下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本发明一部分实施例,而不是全部的实施例。通常在此处附图中描述和示出的本发明实施例的组件可以以各种不同的配置来布置和设计。
因此,以下对在附图中提供的本发明的实施例的详细描述并非旨在限制要求保护的本发明的范围,而是仅仅表示本发明的选定实施例。基于本发明中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。
应注意到:相似的标号和字母在下面的附图中表示类似项,因此,一旦某一项在一个附图中被定义,则在随后的附图中不需要对其进行进一步定义和解释。
在本发明的描述中,需要说明的是,术语“第一”、“第二”、“第三”等仅用于区分描述,而不能理解为指示或暗示相对重要性。
请参照图1,图1为本实施例提供的电子设备100的示意图,本实施例中,所述电子设备100可以是,但不限于,服务器、智能手机、个人电脑(personal computer,PC)、平板电脑、个人数字助理(personal digital assistant,PDA)、移动上网设备(mobile Internet device,MID)等。
所述电子设备100包括接口任务调用装置110、存储器120及处理器130。
所述存储器120以及处理器130各元件相互之间直接或间接地电性连接,以实现数据的传输或交互。例如,这些元件相互之间可通过一条或多条通讯总线或信号线实现电性连接。所述接口任务调用装置110包括至少一个可以软件或固件(firmware)的形式存储于所述存储器120中或固化在所述电子设备100的操作系统(operating system,OS)中的软件功能模块。所述处理器130被配置成执行所述存储器120中存储的可执行模块,例如所述接口任务调用装置110所包括的软件功能模块及计算机程序等。
其中,所述存储器120可以是,但不限于,随机存取存储器(Random Access Memory,RAM),只读存储器(Read Only Memory,ROM),可编程只读存储器(Programmable Read-Only Memory,PROM),可擦除只读存储器(Erasable Programmable Read-Only Memory,EPROM),电可擦除只读存储器(Electric Erasable Programmable Read-Only Memory,EEPROM)等。其中,存储器120被配置成存储程序,所述处理器130在接收到执行指令后,执行所述程序。
所述处理器130可能是一种集成电路芯片,具有信号的处理能力。上述的处理器可以是通用处理器,包括中央处理器(Central Processing Unit,CPU)、网络处理器(Network Processor,NP)等;还可以是数字信号处理器(DSP)、专用集成电路(ASIC)、现场可编 程门阵列(FPGA)或者其他可编程逻辑器件、分立门或者晶体管逻辑器件、分立硬件组件。上述的通用处理器可以实现或者执行本发明实施例中的公开的各方法、步骤及逻辑框图。通用处理器可以是微处理器或者该处理器也可以是任何常规的处理器等。
在本实施例中,将需要访问的目标数据接口封装为回调(callback)任务,即工作线程在需要访问所述目标数据接口时,投递对应的回调任务,所述电子设备100依次处理抽象出来的回调任务。如此,可以在不采用对工作线程加锁的情况下,实现对所述目标数据接口访问的控制,使同一时间只有一个工作线程访问所述目标数据接口。
请参照图2,图2为应用于图1所示的电子设备100的一种接口任务调用方法的流程图,以下将对所述方法包括各个步骤进行详细阐述。
步骤S110,建立任务队列以及用于处理所述任务队列中任务的循环处理函数。
在本实施例中,首先定义一个任务接收对象(例如,Incoming Task Queue对象),该任务接收对象内部创建一个std::queue<PendingTask>的任务队列。该任务队列用来保存从工作线程投递过来的回调任务。因为std::queue结构具有从队列的首部和尾部访问数据更快速的优点,可以在接收到大量投递的回调任务时,可以及时存入队列中。
然后定义工作线程的循环处理函数的对象,即定义一个消息循环MessageLoop对象,该MessageLoop对象是工作线程运行的实际主体。利用消息循环的方式从IncomingTaskQueue对象获取回调任务并执行,每次优先执行MessageLoop中保存的任务,这样就不用每次频繁的从IncomingTaskQueue获取任务,可以提高消息循环的运行速度。
接着调用CreateThread(threadProc)函数创建一个工作线程,threadProc为该工作线程的运行过程接口。然后在threadProc内部运行MessageLoop的Run函数,开启工作线程的消息循环,等待处理callback任务。
步骤S120,接收工作线程投递的回调任务并放入所述任务队列中,其中,所述回调任务由该工作线程需要调用的目标数据接口封装而成,所述回调任务包括相应的优先等级。
在本实施例中,所述工作线程在需要访问目标数据接口时,向所述任务队列投递相应的回调任务。所述回调任务可以根据重要性设置相应的优先等级。例如,所述回调任务可以包括:重要程度较高,需要立即处理的第一等级任务,在本实施例中可以记为PostTask;能够延时处理的第二等级任务,在本实施例中可以记为PostDelayedTask;能够在工作线程空闲时处理的第三等级任务,在本实施例中,可以记为PostIdleWork。
步骤S130,使用所述循环处理函数批量提取并存储所述任务队列中的所述回调任务,并根据所述回调任务的优先等级依次处理所述回调任务。
在本实施例的一种具体实施方式中,请参照图3,所述方法还可以包括步骤S140。
步骤S140,在处理完当前轮次提取出的回调任务后,再次从所述任务队列中批量提取新的未处理的回调任务。如此,实现所述回调任务的循环提取处理。
在本实施例的另一种具体实施方式中,所述循环处理函数可以每间隔一预设时间从所述任务队列中批量提取所述回调任务。如此可实现所述回调任务的循环提取处理。
所述循环处理函数在处理提取出的回调任务时,由于所述第一等级任务重要程度较高,首先对提取出的所述第一等级任务进行处理。
在提取出的回调任务中不存在未处理的所述第一等级任务时,再对所述第二等级任务进行处理。
在提取出的回调任务中不存在未处理的所述第一等级任务及未处理的第二等级任务时,再对提取出的第三等级任务进行处理。也即,所述循环处理函数在检测到提取出的回调任务重没有所述第一等级任务及第二等级任务后,再对所述第二等级任务进行处理。因为所述第三等级任务属于空闲任务,所以在工作线程空闲时用来处理该类型任务,如此可以提高工作线程的利用率,最大程度利用工作线程的处理能力。
可选地,在本实施例中,所述循环处理函数在从所述任务队列中提取回调任务时,若测到所述任务队列中没有回调任务,则执行预设的等待函数,进入休眠状态。当所有回调任务都处理完毕时,MessageLoop对象会暂时调用wait函数,使工作线程进入后台休眠状态。如此可以减少程序运行工作线程的性能消耗。
可选地,在本实施例中,所述循环处理函数在休眠状态时,检测是否有回调任务投递到所述任务队列。当检测到有回调任务投递到所述任务队列时,唤醒所述循环处理函数从所述任务队列提取回调任务并进行处理。即工作线程有回调任务投递过来时,会调用ScheduleWork唤醒当前工作线程的MessageLoop消息循环,继续等待执行回调任务。
基于上述设计,在有多个任务同时需要调用目标数据解救时,本实施例提供的方法可以以最快的速度实现逐个线程调用目标数据接口。并且没有多余的加锁的操作,节约了系统运行资源,提高了多线程并发的处理能力。
请参照图4,本实施例还提供一种应用于图1所示电子设备100的接口任务调用装置110,所述装置包括建立模块111、接收模块112及任务处理模块113。
所述建立模块111,被配置成建立任务队列以及用于处理所述任务队列中任务的循环处理函数。
本实施例中,所述建立模块111可用于执行图2所示的步骤S110,关于所述建立模块111的具体描述可参对所述步骤S110的描述。
所述接收模块112,被配置成接收工作线程投递的回调任务并放入所述任务队列中, 其中,所述回调任务由该工作线程需要调用的目标数据接口封装而成,所述回调任务包括相应的优先等级。
本实施例中,所述接收模块112可用于执行图2所示的步骤S120,关于所述接收模块112的具体描述可参对所述步骤S120的描述。
所述任务处理模块113,被配置成使用所述循环处理函数批量提取并存储所述任务队列中的所述回调任务,并根据所述回调任务的优先等级依次处理所述回调任务。
本实施例中,所述任务处理模块113可用于执行图2所示的步骤S130,关于所述任务处理模块113的具体描述可参对所述步骤S130的描述。
可选地,在本实施例的一种实施方式中,所述装置还包括循环提取模块。
所述循环提取模块,被配置成在处理完当前轮次提取出的回调任务后,再次从所述任务队列中批量提取新的未处理的回调任务。
可选地,在本实施例的另一种实施方式中,所述任务处理模块113通过以下方式提取并存储所述任务队列中的所述回调任务:
每间隔一预设时间从所述任务队列中批量提取所述回调任务。
可选地,在本实施例中,所述回调任务包括重要程度较高需要立即处理的第一等级任务、能够延时处理的第二等级任务及能够在线程空闲时处理的第三等级任务;所述任务处理模块113通过以下方式处理所述回调任务:
对提取出的所述第一等级任务进行处理;
在提取出的回调任务中不存在未处理的所述第一等级任务时,对所述第二等级任务进行处理;
在提取出的回调任务中不存在未处理的所述第一等级任务及未处理的第二等级任务时,对提取出的第三等级任务进行处理。
可选地,在本实施例中,所述任务处理模块被配置成:当所述循环处理函数在检测到所述任务队列中没有回调任务时,执行预设的等待函数,并进入休眠状态。
可选地,在本实施例中,所述任务处理模块还被配置成:
所述循环处理函数在休眠状态时,检测是否有回调任务投递到所述任务队列;
当检测到有回调任务投递到所述任务队列时,唤醒所述循环处理函数从所述任务队列提取回调任务并进行处理。
综上所述,本发明提供的一种接口任务调用方法及装置,通过将需要调用的目标接口封装为回调任务,工作线程在需要调用目标数据接口时,向任务队列投递相应得到回调任务,然后循环处理函数对投递回调任务进行依次处理。如此,本实施例提供的方法可以以 最快的速度实现线程逐个调用访问目标数据接口,并且没有多余的加锁的操作,节约了系统运行资源,提高了多线程并发的处理能力。
本发明实施例提供一种可读存储介质,其上存储有计算机程序,所述计算机程序在执行时实现上述实施例提供的接口任务调用方法。
在本申请所提供的实施例中,应该理解到,所揭露的装置和方法,也可以通过其它的方式实现。以上所描述的装置实施例仅仅是示意性的,例如,附图中的流程图和框图显示了根据本发明的多个实施例的装置、方法和计算机程序产品的可能实现的体系架构、功能和操作。在这点上,流程图或框图中的每个方框可以代表一个模块、程序段或代码的一部分,所述模块、程序段或代码的一部分包含一个或多个用于实现规定的逻辑功能的可执行指令。也应当注意,在有些作为替换的实现方式中,方框中所标注的功能也可以以不同于附图中所标注的顺序发生。例如,两个连续的方框实际上可以基本并行地执行,它们有时也可以按相反的顺序执行,这依所涉及的功能而定。也要注意的是,框图和/或流程图中的每个方框、以及框图和/或流程图中的方框的组合,可以用执行规定的功能或动作的专用的基于硬件的系统来实现,或者可以用专用硬件与计算机指令的组合来实现。
另外,在本发明各个实施例中的各功能模块可以集成在一起形成一个独立的部分,也可以是各个模块单独存在,也可以两个或两个以上模块集成形成一个独立的部分。
所述功能如果以软件功能模块的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本发明的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本发明各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(ROM,Read-Only Memory)、随机存取存储器(RAM,Random Access Memory)、磁碟或者光盘等各种可以存储程序代码的介质。
需要说明的是,在本文中,诸如第一和第二等之类的关系术语仅仅用来将一个实体或者操作与另一个实体或操作区分开来,而不一定要求或者暗示这些实体或操作之间存在任何这种实际的关系或者顺序。而且,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、物品或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、物品或者设备所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括所述要素的过程、方法、物品或者设备中还存在另外的相同要素。
以上所述,仅为本发明的具体实施方式,但本发明的保护范围并不局限于此,任何熟悉本技术领域的技术人员在本发明揭露的技术范围内,可轻易想到变化或替换,都应涵盖在本发明的保护范围之内。因此,本发明的保护范围应所述以权利要求的保护范围为准。
工业实用性
本发明实施例提供的一种接口任务调用方法、装置、电子设备及可读存储介质,通过将需要调用的目标接口封装为回调任务,工作线程在需要调用目标数据接口时,向任务队列投递相应的回调任务,然后循环处理函数对投递回调任务进行依次处理。如此,本实施例提供的方法可以最快的速度实现线程逐个调用访问目标数据接口,并且没有多余的加锁操作,节约了系统运行资源,提高了多线程并发的处理能力。

Claims (17)

  1. 一种接口任务调用方法,应用于电子设备,其特征在于,所述方法包括:
    建立任务队列以及用于处理所述任务队列中任务的循环处理函数;
    接收工作线程投递的回调任务并放入所述任务队列中,其中,所述回调任务由该工作线程需要调用的目标数据接口封装而成,所述回调任务包括相应的优先等级;
    使用所述循环处理函数批量提取并存储所述任务队列中的所述回调任务,并根据所述回调任务的优先等级依次处理所述回调任务。
  2. 根据权利要求1所述的方法,其特征在于,所述方法还包括:
    在处理完当前轮次提取出的回调任务后,再次从所述任务队列中批量提取新的未处理的回调任务。
  3. 根据权利要求1所述的方法,其特征在于,所述使用所述循环处理函数批量提取并存储所述任务队列中的所述回调任务的步骤,包括:
    每间隔一预设时间从所述任务队列中批量提取所述回调任务。
  4. 根据权利要求2或3所述的方法,其特征在于,所述工作线程在需要访问目标数据接口时,向所述任务队列投递相应的回调任务。
  5. 根据权利要求4所述的方法,其特征在于,所述回调任务包括重要程度较高需要立即处理的第一等级任务、能够延时处理的第二等级任务及能够在线程空闲时处理的第三等级任务;所述根据所述回调任务的优先等级依次处理所述回调任务的步骤,包括:
    对提取出的所述第一等级任务进行处理;
    在提取出的回调任务中不存在未处理的所述第一等级任务时,对所述第二等级任务进行处理;
    在提取出的回调任务中不存在未处理的所述第一等级任务及未处理的第二等级任务时,对提取出的第三等级任务进行处理。
  6. 根据权利要求1所述的方法,其特征在于,所述方法还包括:
    所述循环处理函数在检测到所述任务队列中没有回调任务时,执行预设的等待函数,进入休眠状态。
  7. 根据权利要求6所述的方法,其特征在于,所述方法还包括:
    所述循环处理函数在休眠状态时,检测是否有回调任务投递到所述任务队列;
    当检测到有回调任务投递到所述任务队列时,唤醒所述循环处理函数从所述任务队列提取回调任务并进行处理。
  8. 根据权利要求1所述的方法,其特征在于,建立任务队列的步骤包括:
    定义一个任务接受对象,在该任务接受对象内部创建一个任务队列;该任务队列用于保存从工作线程投递过来的回调任务。
  9. 根据权利要求8所述的方法,其特征在于,建立用于处理所述任务队列中任务的循环处理函数的步骤包括:
    定义工作线程的循环处理函数的对象,即定义一个消息循环对象;利用消息循环的方式从任务接受对象获取回调任务并执行,每次优先执行消息循环对象中保存的任务。
  10. 一种接口任务调用装置,应用于电子设备,其特征在于,所述装置包括:
    建立模块,被配置成建立任务队列以及用于处理所述任务队列中任务的循环处理函数;
    接收模块,被配置成接收工作线程投递的回调任务并放入所述任务队列中,其中,所述回调任务由该工作线程需要调用的目标数据接口封装而成,所述回调任务包括相应的优先等级;
    任务处理模块,被配置成使用所述循环处理函数批量提取并存储所述任务队列中的所述回调任务,并根据所述回调任务的优先等级依次处理所述回调任务。
  11. 根据权利要求10所述的装置,其特征在于,所述装置还包括:
    循环提取模块,被配置成在处理完当前轮次提取出的回调任务后,再次从所述任务队列中批量提取新的未处理的回调任务。
  12. 根据权利要求10所述的装置,其特征在于,所述任务处理模块通过以下方式提取并存储所述任务队列中的所述回调任务:
    每间隔一预设时间从所述任务队列中批量提取所述回调任务。
  13. 根据权利要求11或12所述的装置,其特征在于,所述回调任务包括重要程度较高需要立即处理的第一等级任务、能够延时处理的第二等级任务及能够在线程空闲时处理的第三等级任务;所述任务处理模块通过以下方式处理所述回调任务:
    对提取出的所述第一等级任务进行处理;
    在提取出的回调任务中不存在未处理的所述第一等级任务时,对所述第二等级任务进行处理;
    在提取出的回调任务中不存在未处理的所述第一等级任务及未处理的第二等级任务时,对提取出的第三等级任务进行处理。
  14. 根据权利要求10所述的装置,其特征在于,所述任务处理模块被配置成:当所述循环处理函数在检测到所述任务队列中没有回调任务时,执行预设的等待函数,并进入休眠状态。
  15. 根据权利要求14所述的装置,其特征在于,所述任务处理模块还被配置成:
    所述循环处理函数在休眠状态时,检测是否有回调任务投递到所述任务队列;
    当检测到有回调任务投递到所述任务队列时,唤醒所述循环处理函数从所述任务队列提取回调任务并进行处理。
  16. 一种电子设备,其特征在于,包括:
    存储器;
    处理器;
    安装/存储于所述存储器并由所述处理器执行的权利要求10-15中任一项所述的接口任务调用装置。
  17. 一种可读存储介质,其上存储有计算机程序,其特征在于,所述计算机程序在执行时实现上述权利要求1-9中任一项所述的接口任务调用方法。
PCT/CN2017/111927 2017-09-05 2017-11-20 接口任务调用方法、装置、电子设备及可读存储介质 WO2019047376A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201710789002.7 2017-09-05
CN201710789002.7A CN107491350B (zh) 2017-09-05 2017-09-05 接口任务调用方法及装置

Publications (1)

Publication Number Publication Date
WO2019047376A1 true WO2019047376A1 (zh) 2019-03-14

Family

ID=60651414

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2017/111927 WO2019047376A1 (zh) 2017-09-05 2017-11-20 接口任务调用方法、装置、电子设备及可读存储介质

Country Status (2)

Country Link
CN (1) CN107491350B (zh)
WO (1) WO2019047376A1 (zh)

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110633181B (zh) * 2018-06-25 2023-04-07 北京国双科技有限公司 可视化显示方法和装置
CN111435314A (zh) * 2019-01-11 2020-07-21 武汉瓯越网视有限公司 一种不阻塞线程等待异步消息的方法、系统、服务器及存储介质
CN112801559A (zh) * 2021-04-09 2021-05-14 恒生电子股份有限公司 业务请求的处理方法、装置、设备和存储介质

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103605568A (zh) * 2013-10-29 2014-02-26 北京奇虎科技有限公司 一种多线程管理方法及装置
CN105930208A (zh) * 2016-04-01 2016-09-07 广州华多网络科技有限公司 一种线程调度方法及线程调度装置
CN106802826A (zh) * 2016-12-23 2017-06-06 中国银联股份有限公司 一种基于线程池的业务处理方法及装置

Family Cites Families (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101963922B (zh) * 2010-09-29 2012-12-05 用友软件股份有限公司 任务处理方法和装置
CN102455933B (zh) * 2010-10-22 2014-01-15 深圳市科陆电子科技股份有限公司 一种通过线程管理提高多任务处理效率的方法
CN102262564A (zh) * 2011-08-16 2011-11-30 天津市天祥世联网络科技有限公司 视频监控平台系统的线程池结构及实现方法
US20140053157A1 (en) * 2012-08-16 2014-02-20 Microsoft Corporation Asynchronous execution flow
CN105335238B (zh) * 2014-08-12 2020-04-21 腾讯科技(深圳)有限公司 进程间通信方法及装置
CN105718315A (zh) * 2016-02-17 2016-06-29 中国农业银行股份有限公司 一种任务处理方法及服务器

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103605568A (zh) * 2013-10-29 2014-02-26 北京奇虎科技有限公司 一种多线程管理方法及装置
CN105930208A (zh) * 2016-04-01 2016-09-07 广州华多网络科技有限公司 一种线程调度方法及线程调度装置
CN106802826A (zh) * 2016-12-23 2017-06-06 中国银联股份有限公司 一种基于线程池的业务处理方法及装置

Also Published As

Publication number Publication date
CN107491350A (zh) 2017-12-19
CN107491350B (zh) 2018-08-10

Similar Documents

Publication Publication Date Title
WO2016192556A1 (zh) 接口调用方法、装置及终端
EP2831796B1 (en) Persistent and resilient worker processes
WO2019047376A1 (zh) 接口任务调用方法、装置、电子设备及可读存储介质
US10802875B2 (en) Multithread framework for use in pre-boot environment of a system-on-chip
US9336014B2 (en) Method, apparatus, and system for human-machine interaction
US11321125B2 (en) Task priority processing method and processing device
CN107589990B (zh) 一种基于线程池的数据通讯的方法及系统
CN107479981B (zh) 一种基于异步调用实现同步调用的处理方法及装置
WO2019006997A1 (zh) 程序死锁检测方法、存储介质、设备及系统
CN112540806A (zh) 一种小程序页面渲染方法、装置、电子设备及存储介质
CN113010275A (zh) 一种中断处理方法和装置
CN110851276A (zh) 一种业务请求处理方法、装置、服务器和存储介质
CN111984402A (zh) 一种线程池统一调度监控方法及系统
US20210240547A1 (en) Method, apparatus, and electronic device for improving cpu performance
US8756604B2 (en) Async wrapper handling execution of asynchronous operations for synchronous and asynchronous routines
CN108062224B (zh) 基于文件句柄的数据读写方法、装置及计算设备
CN114610467A (zh) 一种多任务超时管理方法及系统
WO2018196459A1 (zh) 一种下载请求处理方法、装置、处理设备及介质
CN115904644A (zh) 任务调度方法、电子设备和计算机程序产品
US11609745B2 (en) Asynchronous data objects for an event driven programming language
Kluge et al. AUTOSAR OS on a message-passing multicore processor
CN116010106A (zh) 数据处理方法和装置
CN109033842B (zh) 数据处理器
CN118093211A (zh) 一种分布式锁的处理方法、装置、设备及介质
CN117971431A (zh) 一种基于虚拟化的任务执行方法、装置、设备及存储介质

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 17924472

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 17924472

Country of ref document: EP

Kind code of ref document: A1