search
HomeBackend DevelopmentPHP Tutorial服务器端PHP多进程编程实战_PHP

服务器端PHP多进程编程实战_PHP

Jun 01, 2016 pm 12:17 PM
phpActual combatserverprogrammingprocess

最近比较PHP跟Python, Erlang的特性,发现PHP有很多人们不常用到的特性。用PHP CLI可以实现很多不错的应用。比如做搜索引擎的爬虫, 长期运行的计算脚本, 完全可以取代其他语言来做服务器的运维。这对于熟悉PHP的人来说如虎添翼。

bitsCN推荐阅读:让PHP开发者事半功倍的十大技巧

为什么PHP多进程很好? 网游服务器大部分都使用多线程而不是多进程的原因也在于进程比线程更加稳定。而且多线程适合现在多核服务器的应用场景,更能发挥多核运算的能力。进程的维护可以用很多操作系统级别的工具。Message Queue解决了多大部分线程通信问题。所以PHP多进程很适合做服务器端的计算密集型的应用。

据一家越南IT公司介绍,他们成功的把PHP后台多进程用在法律文件的分发、处理银行账户的金额这样的企业级的应用上。

使用后台PHP进程可以不影响服务器同时处理网页的请求。这种后台进程一旦发生失败很容易查处原因进行恢复或者补救,所以健壮性更高。不同的进程相互隔离,更加高效,可以统一调度各个服务进程。

PHP是目前应用最广泛的Web开发语言,所以用PHP来做服务器端的应用可以降低成本。可以用现有人员、现有配置、甚至做到代码重用。什么样的场景更适合用PHP后台多进程呢?比如邮件的分发、远程服务的调用、数据的聚合、计划任务、计算结果的缓存这些不需要立即返回的地方。

PHP单进程在某些地方完全可以达到目的,而且更加容易实现,不用考虑进程的同步问题,不用考虑数据的共享问题。PHP CLI(SAPI SERVER API) 命令行接口可以用来做CRON计划任务, 图形界面程序 (使用GTK库)。

PHP CLI例子

<ol class="dp-xml">
<li class="alt"><span><span>php -f test.php  </span></span></li>
<li><span>php -r “echo time();”  </span></li>
<li class="alt"><span>php -R as python style </span></li>
</ol>

PHP读取命令行参数:

  1. php 
  2. #!/usr/bin/php -q  
  3. echo “Test Arguments:\n”;  
  4. echo 

    最近比较PHP跟Python, Erlang的特性,发现PHP有很多人们不常用到的特性。用PHP CLI可以实现很多不错的应用。比如做搜索引擎的爬虫, 长期运行的计算脚本, 完全可以取代其他语言来做服务器的运维。这对于熟悉PHP的人来说如虎添翼。

    bitsCN推荐阅读:让PHP开发者事半功倍的十大技巧

    为什么PHP多进程很好? 网游服务器大部分都使用多线程而不是多进程的原因也在于进程比线程更加稳定。而且多线程适合现在多核服务器的应用场景,更能发挥多核运算的能力。进程的维护可以用很多操作系统级别的工具。Message Queue解决了多大部分线程通信问题。所以PHP多进程很适合做服务器端的计算密集型的应用。

    据一家越南IT公司介绍,他们成功的把PHP后台多进程用在法律文件的分发、处理银行账户的金额这样的企业级的应用上。

    使用后台PHP进程可以不影响服务器同时处理网页的请求。这种后台进程一旦发生失败很容易查处原因进行恢复或者补救,所以健壮性更高。不同的进程相互隔离,更加高效,可以统一调度各个服务进程。

    PHP是目前应用最广泛的Web开发语言,所以用PHP来做服务器端的应用可以降低成本。可以用现有人员、现有配置、甚至做到代码重用。什么样的场景更适合用PHP后台多进程呢?比如邮件的分发、远程服务的调用、数据的聚合、计划任务、计算结果的缓存这些不需要立即返回的地方。

    PHP单进程在某些地方完全可以达到目的,而且更加容易实现,不用考虑进程的同步问题,不用考虑数据的共享问题。PHP CLI(SAPI SERVER API) 命令行接口可以用来做CRON计划任务, 图形界面程序 (使用GTK库)。

    PHP CLI例子

    <ol class="dp-xml">
    <li class="alt"><span><span>php -f test.php  </span></span></li>
    <li><span>php -r “echo time();”  </span></li>
    <li class="alt"><span>php -R as python style </span></li>
    </ol>

    PHP读取命令行参数:

    ___FCKpd___1

    PHP命令行接口标准输入输出:

    <ol class="dp-xml">
    <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
    <li><span>#!/usr/bin/php -q  </span></li>
    <li class="alt"><span>/* Define STDIN in case if it is not already defined by PHP for some reason */  </span></li>
    <li><span>if(!defined(“STDIN”)) {  </span></li>
    <li class="alt"><span>define(“STDIN”, fopen(‘php://stdin’,'r’))  </span></li>
    <li><span>}  </span></li>
    <li class="alt"><span> </span></li>
    <li><span>echo “Hello! What is your name (enter below):\n”;  </span></li>
    <li class="alt">
    <span>$</span><span class="attribute"><font color="#ff0000">strName</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">fread</font></span><span>(STDIN, 80); // Read up to 80 characters or a newline  </span>
    </li>
    <li><span>echo ‘Hello ‘ , $strName , “\n”;  </span></li>
    <li class="alt">
    <span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
    </li>
    </ol>

    CRONJOB可以定时运行某些任务,但要防止重复运行。开始时创建一个锁文件, 结束时删除。或者用ps命令来处理。任务队列可以用MySQL来实现,或者Key/VALUE数据库,或者消息队列来实现。

    进程控制相关函数:

    <ol class="dp-xml">
    <li class="alt"><span><span>Process Control Extensions  </span></span></li>
    <li><span>pcntl_fork()  </span></li>
    <li class="alt"><span>posix_setsid()  </span></li>
    <li><span>posix_kill  </span></li>
    <li class="alt"><span>pcntl_wait  </span></li>
    <li><span>pcntl_signal  </span></li>
    <li class="alt"><span> </span></li>
    <li><span>SIGHUP  </span></li>
    <li class="alt"><span>SIGTERM; system shutdown, kill  </span></li>
    <li><span>SIGINT; sent by Ctrl+c  </span></li>
    <li class="alt"><span>SIGKILL (uncatchable); unresponsive, kill -9  </span></li>
    <li><span>SIGCHLD; child status change  </span></li>
    <li class="alt"><span>SIGSTP; sent by Ctrl+z  </span></li>
    <li><span>SIGCONT; resume from stop, fg </span></li>
    </ol>

    PHP不能对某些错误抛出异常,如何提高PHP多进程应用的容错性?

    ◆可以监控进程,依赖进程失败后报告。

    ◆用CRONJOB实现监控进程。

    ◆将被监控进程PID写成文件。

    ◆定时检查PID文件是否存在 检查ps -o pid=或者file_exists(‘/proc/’)。

    ◆如果线程不存在重启进程。

    回顾以前用Java或者Python做的服务器端的服务都可以用PHP来实现。单一语言更容易维护。以往人们对于Web语言的认识很片面,例如多线程、事 务这些东西都可以改变方式来达到同样的目的。

    原文链接:http://blog.eood.cn/server-side-php-progress-program-best-practice

    SERVER["argc"].”\n”;  
  5. echo 

    最近比较PHP跟Python, Erlang的特性,发现PHP有很多人们不常用到的特性。用PHP CLI可以实现很多不错的应用。比如做搜索引擎的爬虫, 长期运行的计算脚本, 完全可以取代其他语言来做服务器的运维。这对于熟悉PHP的人来说如虎添翼。

    bitsCN推荐阅读:让PHP开发者事半功倍的十大技巧

    为什么PHP多进程很好? 网游服务器大部分都使用多线程而不是多进程的原因也在于进程比线程更加稳定。而且多线程适合现在多核服务器的应用场景,更能发挥多核运算的能力。进程的维护可以用很多操作系统级别的工具。Message Queue解决了多大部分线程通信问题。所以PHP多进程很适合做服务器端的计算密集型的应用。

    据一家越南IT公司介绍,他们成功的把PHP后台多进程用在法律文件的分发、处理银行账户的金额这样的企业级的应用上。

    使用后台PHP进程可以不影响服务器同时处理网页的请求。这种后台进程一旦发生失败很容易查处原因进行恢复或者补救,所以健壮性更高。不同的进程相互隔离,更加高效,可以统一调度各个服务进程。

    PHP是目前应用最广泛的Web开发语言,所以用PHP来做服务器端的应用可以降低成本。可以用现有人员、现有配置、甚至做到代码重用。什么样的场景更适合用PHP后台多进程呢?比如邮件的分发、远程服务的调用、数据的聚合、计划任务、计算结果的缓存这些不需要立即返回的地方。

    PHP单进程在某些地方完全可以达到目的,而且更加容易实现,不用考虑进程的同步问题,不用考虑数据的共享问题。PHP CLI(SAPI SERVER API) 命令行接口可以用来做CRON计划任务, 图形界面程序 (使用GTK库)。

    PHP CLI例子

    <ol class="dp-xml">
    <li class="alt"><span><span>php -f test.php  </span></span></li>
    <li><span>php -r “echo time();”  </span></li>
    <li class="alt"><span>php -R as python style </span></li>
    </ol>

    PHP读取命令行参数:

    ___FCKpd___1

    PHP命令行接口标准输入输出:

    ___FCKpd___2

    CRONJOB可以定时运行某些任务,但要防止重复运行。开始时创建一个锁文件, 结束时删除。或者用ps命令来处理。任务队列可以用MySQL来实现,或者Key/VALUE数据库,或者消息队列来实现。

    进程控制相关函数:

    ___FCKpd___3

    PHP不能对某些错误抛出异常,如何提高PHP多进程应用的容错性?

    ◆可以监控进程,依赖进程失败后报告。

    ◆用CRONJOB实现监控进程。

    ◆将被监控进程PID写成文件。

    ◆定时检查PID文件是否存在 检查ps -o pid=或者file_exists(‘/proc/’)。

    ◆如果线程不存在重启进程。

    回顾以前用Java或者Python做的服务器端的服务都可以用PHP来实现。单一语言更容易维护。以往人们对于Web语言的认识很片面,例如多线程、事 务这些东西都可以改变方式来达到同样的目的。

    原文链接:http://blog.eood.cn/server-side-php-progress-program-best-practice

    SERVER["argv"][0].”\n”;  
  6. ?> 

PHP命令行接口标准输入输出:

___FCKpd___2

CRONJOB可以定时运行某些任务,但要防止重复运行。开始时创建一个锁文件, 结束时删除。或者用ps命令来处理。任务队列可以用MySQL来实现,或者Key/VALUE数据库,或者消息队列来实现。

进程控制相关函数:

___FCKpd___3

PHP不能对某些错误抛出异常,如何提高PHP多进程应用的容错性?

◆可以监控进程,依赖进程失败后报告。

◆用CRONJOB实现监控进程。

◆将被监控进程PID写成文件。

◆定时检查PID文件是否存在 检查ps -o pid=或者file_exists(‘/proc/’)。

◆如果线程不存在重启进程。

回顾以前用Java或者Python做的服务器端的服务都可以用PHP来实现。单一语言更容易维护。以往人们对于Web语言的认识很片面,例如多线程、事 务这些东西都可以改变方式来达到同样的目的。

原文链接:http://blog.eood.cn/server-side-php-progress-program-best-practice

Statement
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
When would you use a trait versus an abstract class or interface in PHP?When would you use a trait versus an abstract class or interface in PHP?Apr 10, 2025 am 09:39 AM

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

What is a Dependency Injection Container (DIC) and why use one in PHP?What is a Dependency Injection Container (DIC) and why use one in PHP?Apr 10, 2025 am 09:38 AM

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Apr 10, 2025 am 09:37 AM

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

How does PHP handle file uploads securely?How does PHP handle file uploads securely?Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?Apr 10, 2025 am 09:33 AM

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.

What is Content Security Policy (CSP) header and why is it important?What is Content Security Policy (CSP) header and why is it important?Apr 09, 2025 am 12:10 AM

CSP is important because it can prevent XSS attacks and limit resource loading, improving website security. 1.CSP is part of HTTP response headers, limiting malicious behavior through strict policies. 2. The basic usage is to only allow loading resources from the same origin. 3. Advanced usage can set more fine-grained strategies, such as allowing specific domain names to load scripts and styles. 4. Use Content-Security-Policy-Report-Only header to debug and optimize CSP policies.

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used?What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used?Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

What is HTTPS and why is it crucial for web applications?What is HTTPS and why is it crucial for web applications?Apr 09, 2025 am 12:08 AM

HTTPS is a protocol that adds a security layer on the basis of HTTP, which mainly protects user privacy and data security through encrypted data. Its working principles include TLS handshake, certificate verification and encrypted communication. When implementing HTTPS, you need to pay attention to certificate management, performance impact and mixed content issues.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use