Home Backend Development PHP Tutorial 服务器端PHP多进程编程实战_PHP

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

Jun 01, 2016 pm 12:17 PM
php Actual combat server programming process

最近比较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>
Copy after login
Copy after login

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>
    Copy after login
    Copy after login

    PHP读取命令行参数:

    ___FCKpd___1
    Copy after login
    Copy after login

    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>
    Copy after login

    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>
    Copy after login

    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>
    Copy after login
    Copy after login

    PHP读取命令行参数:

    ___FCKpd___1
    Copy after login
    Copy after login

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

    ___FCKpd___2
    Copy after login
    Copy after login

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

    进程控制相关函数:

    ___FCKpd___3
    Copy after login
    Copy after login

    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
Copy after login
Copy after login

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

进程控制相关函数:

___FCKpd___3
Copy after login
Copy after login

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 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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Demystifying C: A Clear and Simple Path for New Programmers Demystifying C: A Clear and Simple Path for New Programmers Oct 11, 2024 pm 10:47 PM

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

See all articles