Detailed explanation of PHP using gearman for asynchronous email or SMS sending operations

coldplay.xixi
Release: 2023-04-09 07:22:01
forward
2156 people have browsed it

Detailed explanation of PHP using gearman for asynchronous email or SMS sending operations

The example in this article describes PHP using gearman to perform asynchronous email or SMS sending operations. Share it with everyone for your reference, the details are as follows:

1. Preparation work

1. In order to prevent downtime during business processing, please configure gearman Persistence method.
2. Use gearmanManager to manage our worker scripts to facilitate testing.

Related learning recommendations:PHP programming from entry to proficiency

2. Write test scripts

sendEmail.phpThe code is as follows:

workload(), true); //这里模拟处理过程 //具体的业务,这里应该是请求发送邮件的接口,这里只做演示 sleep(1); echo "workId: {$workId} 发送 {$data['email']} 成功\n"; }
Copy after login

client.phpThe code is as follows:

addServer('127.0.0.1', 4730); $cnt = 5000; $ret = array(); //循环发送5000条邮件 for($i = 0; $i < $cnt; ++$i) { //doBackground异步,返回提交任务的句柄 $ret[$i] = $client->doBackground('sendEmail', json_encode(array( 'email' => "{$i}@qq.com", 'title' => "邮件标题{$i}", 'body' => "我是内容{$i}", ))); }
Copy after login

3. Modify the configuration information in gearmanManager

My gearmanManager is installed under /data/GearmanManager/

> vi /data/GearmanManager/etc/GearmanManager.ini
Copy after login

Add the following information, we start five processes for sendEmail

[sendEmail] ;指定5个进程 dedicated_count=5 ;5个进程都只做sendEmail工作 dedicated_only=1
Copy after login

4. Start gearman

> gearmand -d -q mysql \ --mysql-host=192.168.1.100 \ --mysql-port=3306 \ --mysql-user=gearman \ --mysql-password=123456 \ --mysql-db=gearman \ --mysql-table=gearman_queue &
Copy after login

5. Start gearmanManager

> cd /data/GearmanManager > ./bin/pecl_manager.php -c /data/GearmanManager/etc/GearmanManager.ini -vvv
Copy after login

##6. Run client.php

> /data/php56/bin/php /data/client.php
Copy after login

When we perform ctrl c on pecl_manager.php and forcefully close the worker, client.php can still send requests normally, but the data is saved in mysql.

When we restart the worker, gearman will reload the unprocessed ones for processing.

My mysql is installed on the host machine, and gearman is installed on the virtual machine. If any friends find that gearman cannot connect to mysql, they can temporarily turn off the win10 firewall and enable the ping response of win10. Show.

The above is the detailed content of Detailed explanation of PHP using gearman for asynchronous email or SMS sending operations. For more information, please follow other related articles on the PHP Chinese website!

source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!