


This article mainly introduces in detail how php redis can realize instant push to 2 million users. It has certain reference value. Interested friends can refer to
How to realize instant push to 2 million users. Push, this push can be understood as calling a third-party interface, push, SMS and the like.
At that time, I first wrote a demo to directly read the DB and then push the results individually. . It is conceivable
So I designed a solution based on redis+php multi-process. It is not bad in use but has high scalability, so I will share it.
====================================== ====
The specific logic is as follows: (Ignore my font)
In fact, it can be optimized here. My idea is If there is more user data, you can split the data in redis and use multiple lists. Each list corresponds to multiple php processes, which will be faster.
The following is the specific code I implemented:
Main management script: Just start this when applying.
<?php //push推送配置 注:使用前请确认log文件为空 2016-04-12 include_once(dirname (__FILE__)."/../../config.inc.php"); //if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') != 0) goto check; import('push.class.php'); import('Redis.class.php'); $time =time(); $data = array("apikey"=>'xxxx',"secret"=>'xxxx'); $push = new Channel($data); $redis = new RedisCache($Credis['host'],$Credis['port']); if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') != 0) goto check;//如果有推送任务 直接执行监控代码 /*PUSH配置项*/ $config = array( "file"=>"test.txt", "Title"=>"sssss", "Content"=>"ssssssssssssssss", "OpenType"=>"0", //1是 0否 是否跳转链接 "Url"=>"", //链接地址 "num"=>"500", //每次推送条数 "s"=>"1" //睡眠时间 (单位:秒) ); $num = 15; //启动进程数量 $a = $config['OpenType']==1 ? "是" : "否"; $c = json_encode($config); $info = <<<monkey ************ 请确认信息是否有误*10秒后启动push任务! ************* * 文件名称 : {$config['file']}; * 推送标题 : {$config['Title']}; * 推送内容 : {$config['Content']}; * 是否跳转 : {$config['OpenType']}; * 进程数量 : $num;(如果为单进程无视此项) * 睡眠时间 : {$config['s']}; * 日志目录 : /log; ***************************************************************\n monkey; echo $info; sleep(3); $n = 1; while($n<=10){ echo (10-$n++),"秒\n"; sleep(1); } echo "------------------------- 任务已启动 -------------------------\n"; if($redis->Scount('push_getchannel_success')){ echo "队列有未完成任务\n"; }else{ $res = exec("php redis_getchannel.php {$config['file']}");//写入redis脚本 echo $res; } smtp_mail('xxxx@qq.com','推送任务已开启','请实时监测,5秒后您的手机将接收到测试推送!');//推送监控 实现定时全自动推送 echo "\n---------------- 5秒后 test 将收到测试推送消息 ----------------\n"; sleep(5); $re = $push->BaiduPush('xxxx','xxxxx',$config['Content'],$config['Title'],'1',$config['OpenType'],$config['Url'],'xxxxx',$push); sleep(1); echo "\n---------------- 测试推送已发出!如未收到,请及时终止程序! 10秒后正式推送!!! ----------------\n"; $m = 1; while($m<=10){ echo (10-$m++),"秒\n"; sleep(1); } echo "\n---------------- 推送任务已经开始!请耐心等待! ----------------\n"; //下面设置是否多进程 for($i=1;$i<=$num;$i++){ exec("php redis_push.php '{$c}' > /dev/null 2>&1 &"); } check: while(1){ if(exec('ps aux | grep redis_push.php | grep -v grep | wc -l') == 0){ echo "push 发送完成 用时",time()-$time,"秒"; die(); } echo "当前进程数:",exec('ps aux | grep redis_push.php | grep -v grep | wc -l'),"个","\n"; echo "当前剩余推送数量:".$redis->Scount('push_getchannel_success')."\n"; sleep(10); }
As for writing redis and specific push scripts, just rely on your own imagination and I won’t post them hehehehe
My approach is that the specific push script will automatically terminate and call itself after pushing a certain amount.
Because in actual applications, it is found that php scripts will freeze after running for a long time (maybe due to context switching issues), so I always avoid letting php scripts run for a long time.
Also, the users are definitely not fixed. There will be an increment of 2 million users every day. My plan is to use a scheduled script to organize the increment of users into a user table designed by myself and manage it myself. .
ps: I put all the scripts into a small PHP native framework that I organized for unified management. I will send them out after a while.
The above is the detailed explanation of the example code of PHP redis implementing instant push to 2 million users. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool