[PHP] a way to implement multi process

Time:2021-12-31

Multi process

GOLanguage can easily implement multi process, forPHP, is there a simpler way to implement it?

Here is a clever way to implement multiple processes.

But be carefulMemoryandCPUUsage of

scene: messagePUSH

hypothesisPUSHThere are many types, such as user oriented, game oriented, and global oriented.

We usePHPScript to execute the distribution operation, long connectionPUSHThe service is provided by a third-party systemAPITo support.

PHPThe scripts are executed sequentially, assuming thatNstripPUSH, subsequentPUSHJust wait for the one in frontPUSHExecution is complete before continuing.

For example, Article 1PUSHIs inWhile(true)In such Cycle States, the distribution time range is a whole day; So what elsePUSHJust

Capable of waiting, unable to implement in timePUSHOperation, thus delaying business.

programmeDeamonProcess andActprocess

1、DeamonProcesses: configuringcrontab

* * * * *  flock -xn /tmp/push_deamon.lock -c "/usr/local/php/bin/php push_deamon.php >> /tmp/push_error.log 2>&1"

2、ActProcess: inpush_deamon.phpFor differentpush_id, start the following script.

$command = flock -xn /tmp/push_act_{$push_id}.lock -c "nohup /usr/local/php/bin/php -f push_act.php $push_id >> /tmp/push_error.log 2>&1 &"
exec($command);

problemActThe process was manuallyKill

SomePUSHIt is executed once a day, if the correspondingActThe process was killed.

Then the corresponding lock filepush_act_{$push_id}.lock, it was not cleaned up.

This will lead toDeamonProcess restartActIf a lock file is found during the process, it will not continue to startActProcess.

terms of settlement: inDeamonAutomatically delete yesterday’s lock file in the process

$command = "find /tmp/ -daystart -mtime 1 -type f -name push_act_{$id}.lock -exec rm -f {} \; >> /tmp/push_error.log 2>&1 &";
exec($command);