I'm confused about pcntl_fork
in PHP.
I think it does multi-threading, but how does it work and how would I use it in a script?
I'm confused about pcntl_fork
in PHP.
I think it does multi-threading, but how does it work and how would I use it in a script?
PCNTL can not make threads. It only "forks" current PHP process. What does it mean? When you call
pcntl_fork()
, current process is split into two processes. Whole namespace of parent process is copied into the child and both processes continue with execution in parallel with only one difference:pcntl_fork()
returns child's PID in parent and0
in child.Some hints:
MySQL server has gone away
from all forked processes when first of them closes the connection.Here's example from documentation:
But remember, PHP is just scripting language. It's not designed for parallel computing. You could do better job with simultaneously running CRONs, message queues or program in lower-level language, depending on your needs.
Forked PHP program is very hard to read, understand and debug. Maintaining that program will be a nightmare.
Don't make mistake and avoid forking. You don't need it. What you really need is asynchronous task runner. Good news, there is RabbitMQ and nice tutorial ;-) You can also try promising RabbitMQ library called Bunny
PS: Using message queues instead of forking gives you one more advantage. You can process the queue with multiple servers and scale horizontally as your traffic grows.
EDIT 2019-03-07
I have played a lot with asynchronous concurrency framework
amphp
and I have to mention it here. If you really need to run async non-blocking tasks in single request, I consideramphp
to be the best solution today. It uses concept of php generators ($value = yield $promise
) to execute human-readable code without reactphp-like promise hell.https://amphp.org/