Table of Contents
回复内容:
Home Backend Development PHP Tutorial mysql - 关于开启PHP多进程与它们之间的通信问题

mysql - 关于开启PHP多进程与它们之间的通信问题

Jun 06, 2016 pm 08:45 PM
apache linux mysql nginx php

我们通常在Linux上调用PHP来执行定时任务,如php -f xxx.php,有时候需要先有一个PHP父进程,然后开启多个PHP子进程来做同一件事...

如果这时候当其中一个子进程结束或出现问题时通知父进程,父进程就可以做一些事,如关闭其它正在运行的进程等等吧....

想了解一下大家在此类问题上是怎么实现或处理的?

回复内容:

我们通常在Linux上调用PHP来执行定时任务,如php -f xxx.php,有时候需要先有一个PHP父进程,然后开启多个PHP子进程来做同一件事...

如果这时候当其中一个子进程结束或出现问题时通知父进程,父进程就可以做一些事,如关闭其它正在运行的进程等等吧....

想了解一下大家在此类问题上是怎么实现或处理的?

我一般用php daemon来做,一个进程作为父进程一直跑着(可能是个while + sleep),父进程判断时间到了,就fork一个/多个子进程来处理业务,这时父进程监听子进程退出信号,子进程业务执行完了就退出了,这时父进程会检测到,直到所有子进程退出。

一个原则就是父进程尽可能简单,不要处理业务逻辑,从而保证父进程的稳定性。子进程去处理业务,即使出现FataErr也不会导致所有进程退出。

还有个小提示:信号是可以被覆盖的,也就是说两个子进程退出时,父进程可能只收到一个信号,这时父进程应该用while循环来处理子进程退出事件

http://cn2.php.net/manual/zh/function.pcntl-waitpid.php
php手册还是非常有学习价值的

题主会了吗?我这边用了下pcntl这个方法,貌似要捕获子进程状态的时候父进程就会被阻塞,不过PHP从5.3之后开始支持多线程了

用筷子炒菜不知道是什么样的感觉?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Docker on Linux: Containerization for Linux Systems Docker on Linux: Containerization for Linux Systems Apr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

How does MySQL differ from Oracle? How does MySQL differ from Oracle? Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

What is Maintenance Mode in Linux? Explained What is Maintenance Mode in Linux? Explained Apr 22, 2025 am 12:06 AM

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

NGINX and Apache: Understanding the Key Differences NGINX and Apache: Understanding the Key Differences Apr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

See all articles