mysql - 在PHP中可以通过pcntl_fork函数生成进程,但官方说...
在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模式的时候用最好.

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Compiling code in VSCode is divided into 5 steps: Install the C extension; create the "main.cpp" file in the project folder; configure the compiler (such as MinGW); compile the code with the shortcut key ("Ctrl Shift B") or the "Build" button; run the compiled program with the shortcut key ("F5") or the "Run" button.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

By using shortcut keys or configuration settings, you can implement automatic code typography in Visual Studio Code: Shortcut key typography: Windows/Linux: Ctrl K, Ctrl F; macOS: Cmd K, Cmd F Configuration Settings Typeset: Search and enable "editor.formatOnType", automatically type the current line every time you type a character Advanced typography options: Customize typography rules (e.g., indent size, line length), and select the desired typography (e.g., Prettier, ESLint)

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To set the shortcut keys for Sublime Text, follow these steps: Open the shortcut key settings file Key Bindings - User. Add shortcut key settings using the format { "keys": ["key combination"], "command": "command" }. Save changes. Reload the shortcut key settings for the changes to take effect.
