比如说这么一个web后台页面,要有一个操作,对几万用户发送邮件或者发送推送~要怎么做才能调到php去而且不影响这个操作后的其他操作呢?因为php 这种请求是串行处理的,调用一个函数方法等只有跑完下来才能退出,如果用循环发送,那在发送的时候就只能卡在这个页面不能进行其他操作了,而且找过类似java的多线程实现方法~调一个方法循环发送,然后主进程退出来,调一个接口查看发送的状态,发送了多少,结果是不行的,因为PHP只能并行处理这种web的请求~这个跟php用来做app接口的时候的高并发调用是不同的,因为当php作为这种数据请求的时候,是属于不同用户调用~apache可以做并行~所以要怎么实现呢?
回复内容:
比如说这么一个web后台页面,要有一个操作,对几万用户发送邮件或者发送推送~要怎么做才能调到php去而且不影响这个操作后的其他操作呢?因为php 这种请求是串行处理的,调用一个函数方法等只有跑完下来才能退出,如果用循环发送,那在发送的时候就只能卡在这个页面不能进行其他操作了,而且找过类似java的多线程实现方法~调一个方法循环发送,然后主进程退出来,调一个接口查看发送的状态,发送了多少,结果是不行的,因为PHP只能并行处理这种web的请求~这个跟php用来做app接口的时候的高并发调用是不同的,因为当php作为这种数据请求的时候,是属于不同用户调用~apache可以做并行~所以要怎么实现呢?
这个问题已经找到一个比较简单不用引用库的解决办法了.
http://www.htmlgoodies.com/beyond/javascript/providing-feedback-on-long-running-scripts-in-older-browsers.html
贴出一个网址.
方法的原理是利用session_write_close()函数,当一个长循环在跑的时候,在长循环中加入session_write_close(),关闭一下session~由于php的web service 是针对同一session用户串行的,关闭一次session相当于抛出一次锁,其他服务就可以排队拿到这个锁执行程序~
总的来说相当于一个长循环中每次抛出锁,可以插入其他操作,这样就实现了切换运行~
我目前的做法是,借用redis的队列,把要发送的消息,全部放到里面,然后就不管了
有一个后台发送进程,来处理队列里面的数据
1.如果需要重发,则把发送失败的消息放到一个备份的队列里,每次循环开始前,都把备份队列里的数据放到发送的队列里。
2.php进程不建议常驻,因此,可以把一个进程的生命周期设置为1min,再借用cron来实现进程的重启
对接一个消息队列,把你要处理的任务放入消息队列,简单的可以用redis,复杂点的可以beanstalkd, rabbitmq等
如果坚持用PHP实现,写CLI脚本去这个消息队列拿消息,拿到消息之后处理你的耗时任务
亦可使用其它技术实现,python,java,看你们团队的实际情况和技术栈
PS: PHP有多任务的解决方案,用pthread扩展实现多线程或者pcntl扩展实现多进程,但也不要在web端做这个事情
这个可以借用消息队列做异步处理, 你web后台只是做个触发, 真正的邮件之类的通知,要到异步处理, 这样不会堵塞你web后台的操作,消息队列的话,有很多种方案, 简单点的就是利用redis自己实现一个,或者网上有类似的。
队列处理发送消息的动作的时候, 你可以根据你业务的重要, 比如, 我发送一次,不管成功不成功,无所谓,还是必须把消息发送成功, 必须发送成功的话, 你可以把失败的, 写到另外队列,做处理,或者做log记录之类的, 这个跟公司业务比较接近了。
use swoole

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

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.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor