This article mainly introduces the instructions on PHP multi-process execution tasks. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
Requirements
Yes A specified number of multiple tasks are executed concurrently, and the number of processes is controlled.
Processes & Threads:
The processes are independent of each other and do not affect each other.
Code example:
<?php $task = 0; //任务id $taskNum = 10; //任务总数 $processNumLimit = 2; //子进程总量限制 while(true){ //产生分支 $processid = pcntl_fork(); //创建子进程失败 if( $processid == -1 ){ echo "create process error!\n"; exit(1); } //主进程,获得子进程pid elseif($processid){ $task++; //下一个任务 $currentProcessid = posix_getpid(); //当前进程的Id $parentProcessid = posix_getppid(); // 父级进程的ID $phpProcessid = getmypid(); //当前php进程的id echo "task:",$task,"\tprocessid:",$processid,"\tcurrentProcessid:",$currentProcessid,"\tparentProcessid:",$parentProcessid,"\tphpProcessid:",$phpProcessid,"\n"; //控制进程数 if($task >= $processNumLimit) { echo "wait chl start!\n"; $exitid = pcntl_wait($status); //等待退出 echo "wait chl end!extid:",$exitid,"\tstatus:",$status,"\n"; } //任务总量控制 if($task >= $taskNum) { echo "taskNum enough!\n"; break; } } //processid=0为新创建的进程 else{ //模拟不同任务的不同执行时长 $sleep = rand(1, 5); $currentProcessid = posix_getpid(); //当前进程的Id $parentProcessid = posix_getppid(); // 父级进程的ID $phpProcessid = getmypid(); //当前php进程的id echo "task:",$task,"\tprocessid:",$processid,"\tcurrentProcessid:",$currentProcessid,"\tparentProcessid:",$parentProcessid,"\tphpProcessid:",$phpProcessid,"\tsleep:",$sleep,"\tbegin!\n"; sleep($sleep); echo "task:",$task,"\tprocessid:",$processid,"\tcurrentProcessid:",$currentProcessid,"\tparentProcessid:",$parentProcessid,"\tphpProcessid:",$phpProcessid,"\tsleep:",$sleep,"\tend!\n"; exit(0); //子进程执行完后退出,防止进入循环创建子进程 } }
Result of execution:
task:1 processid:32225 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 task:0 processid:0 currentProcessid:32225 parentProcessid:32224 phpProcessid:32225 sleep:5 begin! task:2 processid:32226 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 wait chl start! task:1 processid:0 currentProcessid:32226 parentProcessid:32224 phpProcessid:32226 sleep:2 begin! task:1 processid:0 currentProcessid:32226 parentProcessid:32224 phpProcessid:32226 sleep:2 end! wait chl end!extid:32226 status:0 task:3 processid:32228 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 wait chl start! task:2 processid:0 currentProcessid:32228 parentProcessid:32224 phpProcessid:32228 sleep:1 begin! task:2 processid:0 currentProcessid:32228 parentProcessid:32224 phpProcessid:32228 sleep:1 end! wait chl end!extid:32228 status:0 task:4 processid:32229 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 wait chl start! task:3 processid:0 currentProcessid:32229 parentProcessid:32224 phpProcessid:32229 sleep:2 begin! task:0 processid:0 currentProcessid:32225 parentProcessid:32224 phpProcessid:32225 sleep:5 end! wait chl end!extid:32225 status:0 task:5 processid:32270 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 wait chl start! task:4 processid:0 currentProcessid:32270 parentProcessid:32224 phpProcessid:32270 sleep:1 begin! task:3 processid:0 currentProcessid:32229 parentProcessid:32224 phpProcessid:32229 sleep:2 end! wait chl end!extid:32229 status:0 task:6 processid:32271 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 wait chl start! task:5 processid:0 currentProcessid:32271 parentProcessid:32224 phpProcessid:32271 sleep:4 begin! task:4 processid:0 currentProcessid:32270 parentProcessid:32224 phpProcessid:32270 sleep:1 end! wait chl end!extid:32270 status:0 task:7 processid:32273 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 wait chl start! task:6 processid:0 currentProcessid:32273 parentProcessid:32224 phpProcessid:32273 sleep:1 begin! task:6 processid:0 currentProcessid:32273 parentProcessid:32224 phpProcessid:32273 sleep:1 end! wait chl end!extid:32273 status:0 task:8 processid:32274 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 wait chl start! task:7 processid:0 currentProcessid:32274 parentProcessid:32224 phpProcessid:32274 sleep:2 begin! task:5 processid:0 currentProcessid:32271 parentProcessid:32224 phpProcessid:32271 sleep:4 end! task:7 processid:0 currentProcessid:32274 parentProcessid:32224 phpProcessid:32274 sleep:2 end! wait chl end!extid:32274 status:0 task:9 processid:32277 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 wait chl start! wait chl end!extid:32271 status:0 task:8 processid:0 currentProcessid:32277 parentProcessid:32224 phpProcessid:32277 sleep:2 begin! task:10 processid:32278 currentProcessid:32224 parentProcessid:15053 phpProcessid:32224 wait chl start! task:9 processid:0 currentProcessid:32278 parentProcessid:32224 phpProcessid:32278 sleep:2 begin! task:8 processid:0 currentProcessid:32277 parentProcessid:32224 phpProcessid:32277 sleep:2 end! task:9 processid:0 currentProcessid:32278 parentProcessid:32224 phpProcessid:32278 sleep:2 end! wait chl end!extid:32277 status:0 taskNum enough
Execution analysis:
After pcntl_fork creates a child process, The child process executes the function code, and the parent process continues to increment the task id and create the next task process;
Wait through pcntl_wait for a child process to finish executing and exit, then create a new process to achieve the total number of processes Control;
After the main process completes creation, exit the loop through break. There may be child processes continuing to execute at this time.
Related functions:
pcntl_fork
Generate a branch (child process) at the current position of the current process. On success, the PID of the generated child process is returned in the parent process execution thread, and 0 is returned in the child process execution thread. On failure, -1 is returned in the parent process context, the child process is not created, and a PHP error is raised.
pcntl_wait
Wait or return the child process status of fork. The wait function scrapes the execution of the current process until a child process exits or receives a signal that requires interrupting the current process or calling a signal handling function. If a child process has exited when this function is called (commonly known as a zombie process), this function returns immediately. All system resources used by the child process will be released.
Others:
1. Problems with zombie processes and orphan processes
Some zombie processes obtain status and release resources by pcntl_wait, and processes that have not been taken over become Orphan process. The orphan process is adopted by the init process to complete wait's status acquisition and resource release.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Laravel5.5 executes the table migration command and the table appears empty Solution
PHP variable scope, global, static and other keywords
The above is the detailed content of Instructions for php multi-process execution tasks. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

大家都知道 Node.js 是单线程的,却不知它也提供了多进(线)程模块来加速处理一些特殊任务,本文便带领大家了解下 Node.js 的多进(线)程,希望对大家有所帮助!

Golang作为一门高并发编程语言,其内置的协程机制和多线程操作实现了轻量级的多任务处理。然而,在多进程处理的场景下,不同进程之间的通信和共享内存成为了程序开发的关键问题。本文将介绍在Golang中实现多进程之间共享内存的应用方法。一、Golang中多进程的实现方式在Golang中,可以通过多种方式实现多进程并发处理,其中包括fork、os.Process、

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

转化方法:1、使用“mb_substr($url,stripos($url,"?")+1)”获取url的参数部分;2、使用“parse_str("参数部分",$arr)”将参数解析到变量中,并传入指定数组中,变量名转为键名,变量值转为键值。

去除方法:1、使用substr_replace()函数将首位数字替换为空字符串即可,语法“substr_replace($num,"",0,1)”;2、用substr截取从第二位数字开始的全部字符即可,语法“substr($num,1)”。

golang是多进程,其线程模型是MPG模型,整体上Go程与内核线程是多对多对应的,因此首先来讲就一定是多线程的。Golang有些所谓的M比N模型,M个线程下可以创建N个go routine,一般而言N远大于M,本质上属于多线程模型,但是协程的调度由Go的runtime决定,强调开发者应该使用channel进行协程之间的同步。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
