Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a swoft2 process

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "what is the swoft2 process", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the swoft2 process"?

Introduction to basic functions

Swoft2 customizes user processes to perform user-defined tasks on a regular basis

Swoft2 supports process pool to solve the problem of multi-process concurrent execution of user-defined tasks

Introduction to processes and threads https://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html

Multi-process programming can solve many meaningful problems, the main process of the program runs core tasks, and some auxiliary processes solve some related functions, such as message queue processing, background periodic task processing, file monitoring, regular log reporting and other related functions. Each process has an independent context and does not interfere with each other.

Configuration options for featur

The configuration of the user process generally needs to be registered in bean.php, and the system will automatically load the custom process configured by the user.

The configuration of the process pool also needs to configure the process pool configuration items in bean.php, but the process pool will not be loaded automatically. You need to execute the command manually to start the user process.

Simple example of user process configuration modifies bean.php 'httpServer' = > [' class' = > HttpServer::class, 'port' = > 18306,' listener' = > ['rpc' = > bean (' rpcServer')], 'process' = > [' crontab' = > bean (CrontabProcess::class) 'log' = > bean (\ App\ Process\ LogProcess::class)],]

The above configuration is the application configuration of the WEB server. If the WebSocket server is written in the wsServer configuration item, just define the process property.

Note that the value of process is an one-dimensional array, in which you can define key yourself, as long as it is legal and does not repeat, the value is your defined process class, please use the factory function bean to load. Simple example of process pool configuration modification bean.php'processPool' = > ['class' = >\ Swoft\ Process\ ProcessPool::class,' workerNum' = > 12]

The above configuration sets the process pool and assigns 12 available work processes. In actual production, you can modify the configuration items according to your project requirements.

The initial value of the process workerid is 0, so the workerid valid access here is 0-11. Please note that the simple example user process code preview namespace App\ Process;use Swoft\ Bean\ Annotation\ Mapping\ Bean;use Swoft\ Log\ Helper\ CLog;use Swoole\ Coroutine;use Throwable;use Swoft\ Process\ Process;use Swoft\ Process\ UserProcess / * Class LogProcess * * @ since 2.0 * * @ Bean () * / class LogProcess extends UserProcess {/ * * @ param Process $process * * @ throws Throwable * / public function run (Process $process): void {$process- > name ('swoft-monitor'); / / set the user process name while (true) {CLog::info (' user process, output every 3 seconds') / / work to be processed Coroutine::sleep (3); / / dormant for 3 seconds}}

Matters needing attention

Custom process classes need to be annotated with Bean

Using custom process classes must inherit Swoft\ Process\ UserProcess

The run method of a user-defined process class is the specific task execution code

The user process needs a while loop to prevent the process from exiting after execution

Hibernation function, please use Coroutine::sleep (3) parameter to configure according to the actual situation of the project, in seconds. Decimal is supported. If the parameter 0.5 is passed, it will be executed once per 500ms.

It is also possible to use\ Swoft\ Timer::tick instead of while (true) in the run method, but tick is more used to solve periodic tasks. This function needs to maintain more call stacks and trigger swoft's built-in events, so it consumes more performance. It is generally used in scenarios where the execution frequency is not high. If some tasks with high execution frequency are recommended to use while (true).

Console print

Process pool code preview namespace App\ Process;use Swoft\ Log\ Helper\ CLog;use Swoft\ Process\ Annotation\ Mapping\ Process;use Swoft\ Process\ Contract\ ProcessInterface;use Swoole\ Coroutine;use Swoole\ Process\ Pool;use Throwable / * Class Worker2Process * * @ since 2.0 * * @ Process (workerId= {0Magne1}) * / class Worker2Process implements ProcessInterface {/ * * @ param Pool $pool * @ param int $workerId * * @ throws Throwable * / public function run (Pool $pool, int $workerId): void {while (true) {CLog::info ('worker-'. $workerId.' Context='.context ()-> getWorkerId (); Coroutine::sleep (3);}

Matters needing attention

The process pool process class needs to be annotated with Process and the parameter is the id array of the assigned process pool work process with workerId value.

The process pool process class needs to implement the Swoft\ Process\ Contract\ ProcessInterface interface.

Process pool Process annotations are not as many workerId as possible, need to be set according to your needs, the more settings, the more memory.

Each workerid is only allowed to be allocated once. If multiple systems are allocated, an exception will be thrown.

The value range of workerid is 0 to bean.php configured workerNum-1. The example of this article is 0-11.

Startup instruction

Foreground enable process

Php bin/swoft process:start

Start the process in the background

Php bin/swoft process:start-d

Restart all worker processes

Php bin/swoft process:reload

Restart

Php bin/swoft process:restart

Out of Service

Php bin/swoft process:stop console print here, I believe you have a deeper understanding of "what is the swoft2 process", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report