CN110795254A - Method for processing high-concurrency IO based on PHP - Google Patents

Method for processing high-concurrency IO based on PHP Download PDF

Info

Publication number
CN110795254A
CN110795254A CN201910898049.6A CN201910898049A CN110795254A CN 110795254 A CN110795254 A CN 110795254A CN 201910898049 A CN201910898049 A CN 201910898049A CN 110795254 A CN110795254 A CN 110795254A
Authority
CN
China
Prior art keywords
coroutine
reactor
processing
swoole
scheduling
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.)
Pending
Application number
CN201910898049.6A
Other languages
Chinese (zh)
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.)
Wuhan Zhimei Interconnection Technology Co Ltd
Original Assignee
Wuhan Zhimei Interconnection 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 Wuhan Zhimei Interconnection Technology Co Ltd filed Critical Wuhan Zhimei Interconnection Technology Co Ltd
Priority to CN201910898049.6A priority Critical patent/CN110795254A/en
Publication of CN110795254A publication Critical patent/CN110795254A/en
Pending legal-status Critical Current

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/54Interprogram communication
    • G06F9/546Message passing systems or structures, e.g. queues
    • 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

Abstract

The invention discloses a method for processing high-concurrency IO based on PHP, which comprises the following steps: the Swoole creates a single or a plurality of processes according to the configuration; the Master process creates N Reactor threads; creating coroutines in each Reactor thread; and asynchronously processing the IO tasks through coroutine scheduling. The method comprises the steps of using a Swoole model, creating a coroutine in each thread through a multithread Reactor model (based on epoll) and a multiprocess Worker, and asynchronously processing IO tasks through coroutine scheduling, namely when IO time consumption is met, processing other code logics through coroutine scheduling until IO is completed and then continuing to execute the previous codes; the system resources are fully utilized, IO blocking waiting and waste of the system resources are reduced, the IO processing capacity under high concurrency is greatly improved, and the problems of Accept performance bottleneck and surprise groups can be solved.

Description

Method for processing high-concurrency IO based on PHP
Technical Field
The invention relates to the technical field of high-concurrency IO processing, in particular to a method for processing high-concurrency IO based on PHP.
Background
High concurrency, which generally refers to the concurrent parallel processing of many requests, is one of the factors that must be considered in the design of a distributed system architecture. When the time for the system to respond to the request is too long, the user experience of the internet product is greatly reduced.
In the internet era, high concurrency generally refers to concurrent access, that is, a plurality of accesses arrive at the same time at a certain time point. Typically, a system has a daily PV of more than ten million, and may be a highly concurrent system.
At present, the most of solutions for concurrent IO use an IO multiplexing technology, and Linux provides select system call for a long time and can maintain 1024 connections in one process. Later, a poll system call is added, and some improvements are made to poll, so that the problem of 1024 limitation is solved, and any number of connections can be maintained. However, select/poll also has a problem: select/poll requires a loop to check the connection for events. At this time, if the server has 100 thousands of connections, only one connection sends data to the server at a certain time, and select/poll needs to be cycled 100 thousands of times, wherein only 1 time is hit, and the remaining 999999 times are invalid, which wastes CPU resources.
This does not really solve the C10K problem until the Linux 2.6 kernel provides a new epoll system call, an unlimited number of connections can be maintained, and no polling is required. Currently, various server programs with high concurrent asynchronous IO are implemented based on epoll, such as Nginx, node. Js, a single process, single threaded program, can maintain over 1 million TCP connections, all due to epoll technology.
Disclosure of Invention
Aiming at the defects existing in the problems, the invention provides a method for processing high-concurrency IO based on PHP.
The invention discloses a method for processing high-concurrency IO based on PHP, which comprises the following steps:
the Swoole creates a single or a plurality of processes according to the configuration;
taking the current process as a Master process, wherein the Master process creates N Reactor threads;
creating coroutines in each Reactor thread;
and asynchronously processing the IO tasks through coroutine scheduling.
As a further improvement of the invention, N is jointly determined by the configured reactivor _ num, worker _ num and the number of CPU cores.
As a further improvement of the present invention, a Reactor object is run in each of the Reactor threads;
the Reactor thread is used for receiving data sent by the client, sending response data from the server and processing a Swoole timer event.
As a further improvement of the present invention, the creating coroutines in each Reactor thread includes:
when each Reactor thread receives a request of a client, a coroutine is created through the Go function of the swoole.
As a further improvement of the present invention, the asynchronous processing of the IO task by coroutine scheduling includes:
when IO blockage occurs in the coroutine, the coroutine gives out control, and the coroutine enters a coroutine scheduling queue to perform asynchronous processing on a blocked task.
Compared with the prior art, the invention has the beneficial effects that:
the method comprises the steps of using a Swoole model, creating a coroutine in each thread through a multithread Reactor model (based on epoll) and a multiprocess Worker, and asynchronously processing IO tasks through coroutine scheduling, namely when IO time consumption is met, processing other code logics through coroutine scheduling until IO is completed and then continuing to execute the previous codes; the system resources are fully utilized, IO blocking waiting and waste of the system resources are reduced, the IO processing capacity under high concurrency is greatly improved, and the problems of Accept performance bottleneck and surprise groups can be solved.
Drawings
FIG. 1 is a flowchart illustrating a method for PHP-based high-concurrency IO processing according to an embodiment of the present invention;
FIG. 2 is a diagram of a Reactor model disclosed in one embodiment of the present invention;
fig. 3 is a flowchart of coroutine scheduling according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be obtained by a person skilled in the art without any inventive step based on the embodiments of the present invention, are within the scope of the present invention.
The invention is described in further detail below with reference to the attached drawing figures:
swoole: PHP asynchronous network communication engine facing production environment.
Coordinating the program: the method is an asynchronous IO Reactor model, the application layer automatically realizes task scheduling, and each currently executed user mode thread is switched by means of the Reactor.
The invention provides a method for processing high-concurrency IO based on PHP, which is characterized in that the IO tasks are processed asynchronously through coroutine scheduling by adding a Reactor model (based on epoll) and a multi-process Worker, so that the IO tasks under the high-concurrency condition are processed efficiently; the method comprises the following steps:
as shown in fig. 1, the present invention provides a method for processing high-concurrency IO based on PHP, which includes:
s1, the Swoole can create single or multiple processes according to the configuration; each process is internally provided with a Master process and a plurality of sub-processes;
s2, taking the current process as a Master process, and creating N Reactor threads by the Master process;
the method specifically comprises the following steps:
when the Swool Server is started, the Master process can create N Reactor threads, wherein N is determined by the actual configuration option, namely, the reactiver _ num, the worker _ num and the actual core number of the CPU;
running a Reactor object in each Reactor thread; and the Reactor thread is used for receiving data sent by the client, sending response data from the server and processing the Swoole timer event.
Further, in the present invention,
as shown in fig. 2, the Reactor model has 4 core operations:
add socket to monitor the reactivor, which may be a listen socket or a client socket, or a pipe, eventfd, signal, etc
The Set modifies the event snooping, and can Set the type of snooping, such as readable and writable. It is well understood that with listen socket is the need for accept when a new client connection arrives. Recv is required for client connections to be data received. A SOCKET is buffered, and if 2M data is to be sent to a client connection, it is not sent out at one time, and the operating system defaults to a TCP buffer of only 256K. 256K can be sent at a time, and the send returns an EAGAIN error after the buffer is full. At this time, the writeable event is to be snooped, and in pure asynchronous programming, the writeable must be snooped to ensure that the send operation is completely non-blocking.
Del is removed from the reactivor and is no longer listening for events
Callback is the corresponding processing logic after the event occurs, and is generally established at add/set.
S2, creating coroutines in each Reactor thread;
the method specifically comprises the following steps:
when each Reactor thread receives a request of a client, a coroutine is created through the Go function of the swoole.
S3, asynchronously processing IO tasks through coroutine scheduling;
the method specifically comprises the following steps:
when IO blockage occurs in the coroutine, the coroutine gives out control, and the coroutine enters a coroutine scheduling queue to perform asynchronous processing on a blocked task, so that the time loss caused by IO blockage is reduced.
As shown in fig. 3, the method of scheduling includes:
1) creating Tsak and transmitting the Tsak into the coroutine;
2) calling a run method;
3) scheduling in a Scheduler cycle;
4) scheduling a return value type;
5) if the type of the return value is Tsak _ Continue, returning to the step 3);
the return value type is Tsak _ Wait, and asynchronous execution is carried out; returning to the step 3) after the execution is finished;
the return value type is Tsak _ Done, and the step 6) is skipped;
6) and ending.
Example (b):
the scheduling of threads and processes is regulated by the operating system, while the scheduling of coroutines is regulated by the user himself. The coroutine scheduler of the swoole can suspend the coroutine before the coroutine A is about to enter the blocked IO operation, such as the read of the socket (which is already set as asynchronous IO), save the current stack information StackA, then switch to the coroutine B, and switch back to the current state of the coroutine A according to the StackA when the IO operation of the coroutine A returns.
S4, ending the coroutine:
when the callback function is finished executing, the coro _ close method is used to finish the coroutine.
The invention has the advantages that:
the method comprises the steps of using a Swoole model, creating a coroutine in each thread through a multithread Reactor model (based on epoll) and a multiprocess Worker, and asynchronously processing IO tasks through coroutine scheduling, namely when IO time consumption is met, processing other code logics through coroutine scheduling until IO is completed and then continuing to execute the previous codes; the system resources are fully utilized, IO blocking waiting and waste of the system resources are reduced, the IO processing capacity under high concurrency is greatly improved, and the problems of Accept performance bottleneck and surprise groups can be solved.
The above is only a preferred embodiment of the present invention, and is not intended to limit the present invention, and various modifications and changes will occur to those skilled in the art. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (5)

1. A method for processing high-concurrency IO based on PHP is characterized by comprising the following steps:
the Swoole creates a single or a plurality of processes according to the configuration;
taking the current process as a Master process, wherein the Master process creates N Reactor threads;
creating coroutines in each Reactor thread;
and asynchronously processing the IO tasks through coroutine scheduling.
2. The method of claim 1, wherein N is determined by a configured reactivnum, worker num and a number of CPU cores.
3. The method of claim 1, wherein one Reactor object runs in each of the Reactor threads;
the Reactor thread is used for receiving data sent by the client, sending response data from the server and processing a Swoole timer event.
4. The method of claim 1, wherein creating coroutines in each Reactor thread comprises:
when each Reactor thread receives a request of a client, a coroutine is created through the Go function of the swoole.
5. The method of claim 1, wherein the asynchronously processing IO tasks through coroutine scheduling comprises:
when IO blockage occurs in the coroutine, the coroutine gives out control, and the coroutine enters a coroutine scheduling queue to perform asynchronous processing on a blocked task.
CN201910898049.6A 2019-09-23 2019-09-23 Method for processing high-concurrency IO based on PHP Pending CN110795254A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910898049.6A CN110795254A (en) 2019-09-23 2019-09-23 Method for processing high-concurrency IO based on PHP

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910898049.6A CN110795254A (en) 2019-09-23 2019-09-23 Method for processing high-concurrency IO based on PHP

Publications (1)

Publication Number Publication Date
CN110795254A true CN110795254A (en) 2020-02-14

Family

ID=69439815

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910898049.6A Pending CN110795254A (en) 2019-09-23 2019-09-23 Method for processing high-concurrency IO based on PHP

Country Status (1)

Country Link
CN (1) CN110795254A (en)

Cited By (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111431969A (en) * 2020-02-28 2020-07-17 平安科技(深圳)有限公司 Unified deployment system and method for connection pool
CN111427584A (en) * 2020-04-14 2020-07-17 北京达佳互联信息技术有限公司 Method, device and equipment for generating executable file
CN111935101A (en) * 2020-07-16 2020-11-13 北京首汽智行科技有限公司 Communication protocol design method between client and server
CN112416353A (en) * 2020-08-10 2021-02-26 上海幻电信息科技有限公司 Channel package packaging method and device and computer equipment
CN112612428A (en) * 2020-12-31 2021-04-06 上海英方软件股份有限公司 Method and device for improving performance of Codeigniter frame
CN112954006A (en) * 2021-01-26 2021-06-11 重庆邮电大学 Industrial Internet edge gateway design method supporting Web high-concurrency access
CN113127204A (en) * 2021-04-29 2021-07-16 四川虹美智能科技有限公司 Method and server for processing concurrent services based on reactor network model
CN113162932A (en) * 2021-04-23 2021-07-23 航天新通科技有限公司 Asynchronous I/O operation method and device based on socket
WO2024036874A1 (en) * 2022-08-17 2024-02-22 奇安信网神信息技术(北京)股份有限公司 Data processing method and system, electronic device and computer readable storage medium

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108427553A (en) * 2018-03-30 2018-08-21 吉旗(成都)科技有限公司 A kind of pure PHP web frames based on IO multiplexings
CN108647087A (en) * 2018-05-17 2018-10-12 腾讯科技(深圳)有限公司 Realize the reentrant method, apparatus of PHP kernels, server and storage medium
CN109787997A (en) * 2019-02-26 2019-05-21 上海易点时空网络有限公司 TCP method of servicing and server based on PHP

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108427553A (en) * 2018-03-30 2018-08-21 吉旗(成都)科技有限公司 A kind of pure PHP web frames based on IO multiplexings
CN108647087A (en) * 2018-05-17 2018-10-12 腾讯科技(深圳)有限公司 Realize the reentrant method, apparatus of PHP kernels, server and storage medium
CN109787997A (en) * 2019-02-26 2019-05-21 上海易点时空网络有限公司 TCP method of servicing and server based on PHP

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
翁支和: "swoole基础+easyswoole实践", pages 1 - 13 *
黑马程序员编著: "Python实战编程:从零学Python", 北京:中国铁道出版社, pages: 363 - 365 *

Cited By (12)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111431969A (en) * 2020-02-28 2020-07-17 平安科技(深圳)有限公司 Unified deployment system and method for connection pool
CN111427584A (en) * 2020-04-14 2020-07-17 北京达佳互联信息技术有限公司 Method, device and equipment for generating executable file
CN111935101A (en) * 2020-07-16 2020-11-13 北京首汽智行科技有限公司 Communication protocol design method between client and server
CN112416353A (en) * 2020-08-10 2021-02-26 上海幻电信息科技有限公司 Channel package packaging method and device and computer equipment
CN112612428A (en) * 2020-12-31 2021-04-06 上海英方软件股份有限公司 Method and device for improving performance of Codeigniter frame
CN112612428B (en) * 2020-12-31 2022-06-28 上海英方软件股份有限公司 Method and device for improving performance of Codeigniter frame
CN112954006A (en) * 2021-01-26 2021-06-11 重庆邮电大学 Industrial Internet edge gateway design method supporting Web high-concurrency access
CN112954006B (en) * 2021-01-26 2022-07-22 重庆邮电大学 Industrial Internet edge gateway design method supporting Web high-concurrency access
CN113162932A (en) * 2021-04-23 2021-07-23 航天新通科技有限公司 Asynchronous I/O operation method and device based on socket
CN113162932B (en) * 2021-04-23 2023-01-06 航天新通科技有限公司 Asynchronous I/O operation method and device based on socket
CN113127204A (en) * 2021-04-29 2021-07-16 四川虹美智能科技有限公司 Method and server for processing concurrent services based on reactor network model
WO2024036874A1 (en) * 2022-08-17 2024-02-22 奇安信网神信息技术(北京)股份有限公司 Data processing method and system, electronic device and computer readable storage medium

Similar Documents

Publication Publication Date Title
CN110795254A (en) Method for processing high-concurrency IO based on PHP
US6560626B1 (en) Thread interruption with minimal resource usage using an asynchronous procedure call
US9436510B2 (en) System and method for managing the interleaved execution of threads
CN108595282A (en) A kind of implementation method of high concurrent message queue
WO2021000758A1 (en) Robotic resource task cycle management and control method and apparatus
CN112465129A (en) On-chip heterogeneous artificial intelligence processor
CN112463709A (en) Configurable heterogeneous artificial intelligence processor
CN110471777B (en) Method and system for realizing multi-user sharing and using Spark cluster in Python-Web environment
CN107046510B (en) Node suitable for distributed computing system and system composed of nodes
CN108710535A (en) A kind of task scheduling system based on intelligent processor
US20120297216A1 (en) Dynamically selecting active polling or timed waits
CN103365718A (en) Thread scheduling method, thread scheduling device and multi-core processor system
CN103092682A (en) Asynchronous network application program processing method
WO2014110702A1 (en) Cooperative concurrent message bus, driving member assembly model and member disassembly method
CN111427751A (en) Method and system for processing service based on asynchronous processing mechanism
CN108073414B (en) Implementation method for merging multithreading concurrent requests and submitting and distributing results in batches based on Jedis
CN102904961A (en) Method and system for scheduling cloud computing resources
US20230127112A1 (en) Sub-idle thread priority class
US20110173287A1 (en) Preventing messaging queue deadlocks in a dma environment
CN115964153A (en) Asynchronous task processing method, device, equipment and storage medium
JP4584935B2 (en) Behavior model based multi-thread architecture
EP0614139A2 (en) External procedure call for distributed processing environment
CN109388501B (en) Communication matching method, device, equipment and medium based on face recognition request
US10523746B2 (en) Coexistence of a synchronous architecture and an asynchronous architecture in a server
CN110928746B (en) Configurable business processing system, method, device and storage medium

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