本文实例讲述了PHP swoole和redis异步任务实现方法。分享给大家供大家参考,具体如下:
redis异步任务
interface.php
<"zhezhao[".$i."]"; $redis = new Redis(); $redis->connect("127.0.0.1"); $redis->publish("test",$msg); $redis->close(); }
handler.php
<"127.0.0.1"); $redis->subscribe(array("test"), 'handleFun'); function handleFun($redis, $chan, $data) { write($data); } function write($data){ $path = "/tmp/mailList-redis.log"; $str = "[".date("Y-m-d H:i:s")."]".$data; $str .= PHP_EOL; file_put_contents($path,$str,FILE_APPEND); }
swoole异步任务
interface.php
<"zhezhao[".$i."]"; $client = new swoole_client(SWOOLE_SOCK_TCP); $client->connect('127.0.0.1', 9501, 0.5); $client->send($msg); $client->close(); }
handler.php
<"127.0.0.1", 9501); $serv->set(array('task_worker_num' => 4)); $serv->on('receive', function($serv, $fd, $from_id, $data) { $task_id = $serv->task($data); }); $serv->on('task', function ($serv, $task_id, $from_id, $data) { handle($data); $serv->finish($data); }); $serv->start(); function handle($data){ sleep(2); mailLog("Send Mail successfully to $data"); } function mailLog($str){ $path = "/tmp/mailList.log"; $str = "[".date("Y-m-d H:i:s")."]".$str; $str .= PHP_EOL; file_put_contents($path,$str,FILE_APPEND); }
比较
redis异步任务日志
swoole异步任务日志
通过对比任务日志我们可以看到,由于swoole开了4个进程执行异步任务,所以处理异步任务的效率大概是redis的四倍,如果swoole只开一个进程的话,效率和redis几乎没有什么差别。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP进程与线程操作技巧总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 国风大师纯音系列《古筝仙子 常静演奏 十指弹一》1CD[FLAC][919MB]
- DasKapital-OneMustHaveChaosInsidetoGiveBirthtoaDancingStar(2024)[24-44.1]WAV
- 王菲.1999-《只爱陌生人》日本东芝版[低速原抓WAV+CUE]
- 张小英1987-金奖金曲第一集[新加坡版][WAV+CUE]
- 鹿晗.2015-重启【海蝶】【FLAC分轨】
- 张芸京.2012-小女孩【金牌大风】【WAV+CUE】
- 余天.2002-雄狮魅力金曲2CD(南方金点系列)【南方】【WAV+CUE】
- 银霞.1982-《结·SAYYESMYBOY》台湾复刻版[WAV+CUE]
- 梁汉文1993-火热动感[引进版][WAV+CUE]
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】