PHP异步调用的实现方式
比如现在有个需求,就是给大量用户发送邮件:
<code>$i=0; while ($i </code>
对于以上事例代码的用户体验就很差,实际上也没人会这么干,那么怎样使用程序后台异步执行,而又不影响用户的体验呢?
求思路...
回复内容:
比如现在有个需求,就是给大量用户发送邮件:
<code>$i=0; while ($i </code>
对于以上事例代码的用户体验就很差,实际上也没人会这么干,那么怎样使用程序后台异步执行,而又不影响用户的体验呢?
求思路...
我们使用的是laravel框架 + beanstalkd 队列,网上有安装和使用的说明.
youku里有一个laravel框架的视频教程,有一章就是讲队列的.
简单点,你按照那上面来做一遍就行了.
http://v.youku.com/v_show/id_XNzgzNjQ2ODQ0.html
Laravel+Redis也行,你自己先试试,不明白继续提问.
还有一种方式,你用swoole框架. 用里面的异步调用.详见:www.swoole.com
或者更简单的方式,但不适合你这种情况,但可以参考:http://www.laruence.com/2008/04/14/318.html
你看着三种方式哪种更适合些?
用redis服务 消息队列做两个php进程的之间的通讯,实现多个进程异步执行
使用消息队列。
1、建立一个邮件队列数据表和一个插入邮件队列的PHP公共函数
2、在需要发送邮件的地方,调用上面的PHP公共函数,将邮件先写到邮件队列中。
3、用crontab建立一个定时任务,定期扫描邮件队列,如果有未发送的邮件,则进行发送
其实示例代码没有错,实际上也就是这么做的,只不过是作为单独一个进程来执行的,就像楼上几位说的一样。但是这依然是用阻塞模式运行的。
redis队列和php多进程,和一楼思路一样的,之前项目中也是用的这种方式。
推荐Gearman,百万级生产环境跑过。
我们的shopex为了兼容各种虚拟主机曾经这么干过:
页面下面挂个
ignore_user_abort(true);
set_time_limit(0);
header('Content-Lenght: 6');
echo 'void()';
...干一部分活...
curl / fsockopen 请求自己...
别忘了, 开头加个锁, 避免同时干活的太多.
使用Linux的CRON任务或者使用fsockopen来实现或者CURL
可以使用第三方邮件发送服务平台

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

Validator can be created by adding the following two lines in the controller.
