Table of Contents
回复内容:
Home Backend Development PHP Tutorial mysql - 在PHP中可以通过pcntl_fork函数生成进程,但官方说...

mysql - 在PHP中可以通过pcntl_fork函数生成进程,但官方说...

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

在PHP中可以通过pcntl_fork函数生成进程,但官方说当其被用于Web服务环境时可能会带来意外的结果。 不知道你们用过没有,会有什么意外后果?

因为我想通过PHP来完成一件事,但是我想通过启动多个PHP进程来一起完成这件事,于是就想到了pcntl_fork函数,但对进程又不了解,不知道这种想法是否可行,如果可行的话,是怎么实现的呢?

回复内容:

在PHP中可以通过pcntl_fork函数生成进程,但官方说当其被用于Web服务环境时可能会带来意外的结果。 不知道你们用过没有,会有什么意外后果?

因为我想通过PHP来完成一件事,但是我想通过启动多个PHP进程来一起完成这件事,于是就想到了pcntl_fork函数,但对进程又不了解,不知道这种想法是否可行,如果可行的话,是怎么实现的呢?

总是会有人批评PHP进程常驻时内存管理等的问题,不过:

https://github.com/CoderKungfu/php-queue

https://github.com/chrisboulton/php-resque

这两个库的模式都是用一个只负责fork的主进程配合fork出来的worker进程来实现可靠/易管理的“常驻”,主进程干的事少,而worker干完活就退出了,所以,可以放心用。

apache、 fastcgi、php-fpm 模式下生的子进程都是有生命周期的,超时后可能会被干掉,或者变狠成僵尸。

直接可以通过popen函数来实现你的需求。我经常这样做

这样生成的进程难以管理,官方也不建议使用的

这种做法违背了php的语言特性,php一次请求到响应结束就是一个生命周期

pcntl系的函数,个人建议是跑php的cli模式的时候用最好.

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

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

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.

CentOS: Security, Stability, and Performance CentOS: Security, Stability, and Performance Apr 21, 2025 am 12:11 AM

CentOS is the first choice for server and enterprise environments for its superior security, stability and performance. 1) Security provides forced access control through SELinux to improve system security. 2) Stability is supported by the LTS version for up to 10 years to ensure the stability of the system. 3) Performance significantly improves system response speed and resource utilization by optimizing kernel and system configuration.

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.

Linux Operations: Managing Files, Directories, and Permissions Linux Operations: Managing Files, Directories, and Permissions Apr 23, 2025 am 12:19 AM

In Linux, file and directory management uses ls, cd, mkdir, rm, cp, mv commands, and permission management uses chmod, chown, and chgrp commands. 1. File and directory management commands such as ls-l list detailed information, mkdir-p recursively create directories. 2. Permission management commands such as chmod755file set file permissions, chownuserfile changes file owner, and chgrpgroupfile changes file group. These commands are based on file system structure and user and group systems, and operate and control through system calls and metadata.

Linux: A Deep Dive into Its Fundamental Parts Linux: A Deep Dive into Its Fundamental Parts Apr 21, 2025 am 12:03 AM

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

See all articles