关于PHP的异步调用
众所周知,PHP没有多线程这种东西,虽然也可以实现异步,但都是用一些折中的方法来做到的。
总结一下自己这几天接触到的PHP异步调用需求和解决方法。
当然,自己是个伪码农,使用的方法,都是些极端不优雅的笨方法,还有待总结修正提高。
一、一个专利采集分析的系统,需要一个完整的操作界面,一个采集进度的动态进度条。(AJAX)
用AJAX来实现,通过ajax不停地访问服务器,,通过setInterval来设置间隔时间,访问backend.php文件,获得已经采集的数量,然后更新页面相应DOM的内容即可。
jQuery学得不太好,代码比较丑陋,demo如下:
<span style="color: #008080;"> 1</span> jQuery(document).ready(<span style="color: #0000ff;">function</span><span style="color: #000000;">($) { </span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> $('#submit').click(<span style="color: #0000ff;">function</span><span style="color: #000000;">(){ </span><span style="color: #008080;"> 4</span> setInterval("updateMsg()", 1000<span style="color: #000000;">); </span><span style="color: #008080;"> 5</span> $.post('total.php', $('#form1').serialize(), <span style="color: #0000ff;">function</span><span style="color: #000000;">(data, textStatus){ </span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">var</span> new_data = "<p>本次所要采集的专利总数为:" + data + "</p>"<span style="color: #000000;">; </span><span style="color: #008080;"> 7</span> $('#total_area'<span style="color: #000000;">).html(new_data); </span><span style="color: #008080;"> 8</span> $('#monitor_area').html('<p>正在初始化信息监控.....</p>'<span style="color: #000000;">); </span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">10</span> $.post('test.php',$('#form1'<span style="color: #000000;">).serialize()); </span><span style="color: #008080;">11</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">; </span><span style="color: #008080;">12</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> updateMsg(){ </span><span style="color: #008080;">15</span> $.get("backend.php",{},<span style="color: #0000ff;">function</span><span style="color: #000000;">(data, textStatus){ </span><span style="color: #008080;">16</span> <span style="color: #0000ff;">var</span> now_total = "<p>目前已采集数量:" + data + "</p>"<span style="color: #000000;">; </span><span style="color: #008080;">17</span> $("#monitor_area"<span style="color: #000000;">).html(now_total); </span><span style="color: #008080;">18</span> <span style="color: #000000;"> }); </span><span style="color: #008080;">19</span> }
二、一个邮件发送提醒的应用。(消息队列)
一个报名系统,想在第一时间知道报名者的信息并与其取得联系,并发极低,可能十多天就那么一条报名信息。
手机提醒用了发邮件到139的方法。
但是有一个问题,就是将发邮件的代码写到用户提交个人信息的程序段里以后,提交的过程会变得非常慢,可能达到3S多,简直无法忍受。
对于邮件发送这种耗时很长的东西,采用了“队列”的方法。当然,这个队列没有RabbitMQ和ZeroMQ这种东西这么高级,其实就是将信息存到数据库里,算作是入队列了,然后设置一个cron来处理数据库里的这些信息,处理了,也就是出队列的,这也是个笨办法了。
三、那些听说过没有用过的高级方法
1.自然就是到处都是的消息队列了,自己用数据库模拟的,只不过是最低端的方法而已,不是针对并发的,若是面对高并发,必然会挂掉。这个时候,用上传说中的RabbitMQ这些东西,性能应该有极大的提升。还有就是Redis数据库,用过这个东西,感觉用它的list来做消息队列,应该也是非常棒的。
2.CURL的方法,curl_multi据说也是个好东西,但是由于CUROPT_TIMEOUT最小是1,所以客户端至少要等待1S,这也是硬伤。
3.popen()函数,打开一个指向进程的管道,该进程由派生给定的 command 命令执行而产生。
<span style="color: #008080;">pclose</span>(<span style="color: #008080;">popen</span>("/home/xinchen/backend.php &", 'r'));
4.fsockopen()方法,这个方法要自己拼接处http头来才行。
5.PHP多进程,其实这个方法自己用过,就是将要处理的大段数据按照for循环,用vim处理分成小段,然后在CLI模式下跑
php –f example1.php &<span style="color: #000000;"> php –f example2.php </span>&
用这种笨办法将PHP的进程放到后台来执行……
后来才知道,原来PHP在*uinx下可以直接pcntl类似于C那样fork出进程来,才知道了这个方法,在采集数据的时候,挺好用的,配合CURL和fsockopen,速度飞快。可惜,可惜的是,刚爽了一会,然后IP就被封掉了……
这个的处理,还是很方便的。
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 0; <span style="color: #800080;">$i</span> $intNum; <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { </span><span style="color: #008080;"> 2</span> <span style="color: #800080;">$pids</span>[<span style="color: #800080;">$i</span>] = pcntl_fork();<span style="color: #008000;">//</span><span style="color: #008000;"> 产生子进程,而且从当前行之下开试运行代码,而且不继承父进程的数据信息</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$pids</span>[<span style="color: #800080;">$i</span>] == -1<span style="color: #000000;">) { </span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">echo</span> "couldn't fork". "\n"<span style="color: #000000;">; </span><span style="color: #008080;"> 5</span> } <span style="color: #0000ff;">elseif</span>(!<span style="color: #800080;">$pids</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]) { </span><span style="color: #008080;"> 6</span> <span style="color: #008080;">sleep</span>(1<span style="color: #000000;">); </span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">echo</span> "\n"."第".<span style="color: #800080;">$i</span>."个进程 -> " . <span style="color: #008080;">time</span>(). "\n"<span style="color: #000000;">; </span><span style="color: #008080;"> 8</span> <span style="color: #008000;">//</span><span style="color: #008000;">这里就可以放信息采集抓取等东西的代码了。</span> <span style="color: #008080;"> 9</span> <span style="color: #0000ff;">exit</span>(0);<span style="color: #008000;">//</span><span style="color: #008000;">子进程要exit否则会进行递归多进程,父进程不要exit否则终止多进程</span> <span style="color: #008080;">10</span> <span style="color: #000000;"> } </span><span style="color: #008080;">11</span> }
6.gearman分布式计算,开很多的worker来支持将job分布到不同机器上去执行,这个,自己接触不到,传说中了。
参考的那些资料:
1.当然是鸟哥的博客了,风雪之隅,这几种异步方法都是在他那儿看到的。
2.张宴的博客,多进程的内容。

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

AI Hentai Generator
Generate AI Hentai for free.

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

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

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

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

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

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

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.

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
