1. Scheduled execution
It’s crontab, Linux command, how to use it, google it yourself. I just want to say that you need to pay attention to permissions when using crontab. Operation and maintenance often use root to start, resulting in some files that web users do not have permissions for.
2. Background guard
It is divided into two steps: 1. You need to write an infinite loop in the script, because php is not like python. I usually do {....} while (true); but generally add a sleep, otherwise the machine will be killed; 2. When starting the process, you need to add an "&" at the end. You can also google this. If you need to record the output information, you need to write php xxxx.php > /tmp/phplog & , so that the program information is recorded in the file, making it easier to troubleshoot problems later.
3. Operation monitoring
The background daemon process is started, but you cannot be 100% sure that your program will not display warning messages. Once these messages appear, PHP will terminate the current process. At this time, the background program will directly quit. So in addition to the program that handles things normally, you also need a program that checks the running status of that program. I usually call it xxxxDefend.php. An example of this program is as follows
#!/usr/local/php5/bin/php
php
//Startup command
$action = '/usr/local/php5/bin/php xxxxxx.php';
$logPath = '/tmp/logs/';
do {
$result = array ();
exec("ps aux | grep 'xxxxxx.php'", $result);
$isOk = 0;
foreach ($result as $v) {
$is = strpos($v, $action );
if (false !== $is) {
$isOk++;
}
}
$exec = $action . " > " . $logPath . "xxxxxx_log &";
for ($i=1; $i exec($exec);
}
sleep(5);
} while (true);
Start this program in the background as well, and it will Every 5 seconds, the ps command will be used to check whether the working program exists. If it is not there or there are less than 5, it will start up to 5. Of course, this work can also be left to operation and maintenance using the shell, but when the operation and maintenance capabilities are insufficient, you have to do it yourself.
4. Multi-process
In order to increase efficiency, the working PHP will usually start multiple processes at the same time, or even run on multiple machines. At this time, we must consider the problem of multiple processes processing the same data at the same time. At this time, I usually make a queue for the tasks (usually using redis, which has pretty good performance. How to do it, you can google it), and then the working program pops out a record to be worked on each time. For example, if you have a large Files need to be processed. At this time, I usually process the files and put them one by one into the redis list, so that the working program can be popped by multiple programs at the same time and executed in parallel without duplication. If you really don’t have redis, you can use mysql to build an innodb table. Before the program processes the data, be sure to add a read lock to the data to be processed, and then add a mark after processing, or directly delete the data. This can also be achieved The problem of non-duplication in multiple processes.
5. Log
The background program will usually keep running, and basically no one will care about it unless something goes wrong, so the log is extremely important, because once something goes wrong, you need to rely on the log to find the cause, unlike the front-end program. echo to see where the error is. Don’t be afraid of logging too much or wasting space. Hard drives are worthless, but a bug may directly affect your income. I usually keep logs like this
[Machine IP] [Process pid] [Time] [Current program file name] [Number of file lines] [Necessary parameters and information] [Others]
These are what you can think of in a normal program Problem, usually a try catch is added to the outermost layer of the program, so that most exceptions can be caught and then recorded (the warning catch is not available, which is quite depressing)
6. Performance optimization
A background like this Generally, the operation and maintenance of programs will be done separately for the machine. At this time, you need to do a stress test to see how many processes the machine can run. This is usually done when the processor is busy processing tasks, looking at the machine's CPU, memory, network, and For the usage of the hard disk, it is best to reach the maximum value at the same time, so that your machine is not wasted. If the hard disk usage is very high and the others are very low, you need to optimize the program. In this case, it is usually the case of reading and The written data is temporarily stored in the memory for a period of time, and then written to the hard disk at once; if the CPU is extremely high, it means that your algorithm is too messy, so optimize it; memory and network generally will not become bottlenecks, and PHP will not be used much. Memory and the server must have at least a gigabit network card. These two items are generally not bottlenecks. So I usually open a memcache on these machines, haha, no waste.
7. Digression
A question about server close_wait. PHP programmers are generally not very rigorous. Few people will actively close the connection after opening it. For example, when connecting to a database or memcache, many programmers will create a link, operate it, and then the program execution will be completed. If PHP does not actively close the connection, the other party's machine will always wait for the closing operation. What you see on the other party's server is a close_wait state, and a machine can only open more than 60,000 links, especially After the background program started running, the other party's machine was quickly filled up and could not be connected. At this time, both sides need to make some modifications. On the one hand, PHP needs to actively disconnect, and on the other hand, the other party's machine needs to change the default timeout of close_wait to a shorter time (how to change it? Google it yourself). I usually only use memcache. 5 seconds, the database is longer, which is 2 minutes. After this processing, the server's continuity will be greatly increased, and the concurrency capability will also be improved.

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

Implementing an array sliding window in PHP can be done by functions slideWindow and slideWindowAverage. 1. Use the slideWindow function to split an array into a fixed-size subarray. 2. Use the slideWindowAverage function to calculate the average value in each window. 3. For real-time data streams, asynchronous processing and outlier detection can be used using ReactPHP.

The __clone method in PHP is used to perform custom operations when object cloning. When cloning an object using the clone keyword, if the object has a __clone method, the method will be automatically called, allowing customized processing during the cloning process, such as resetting the reference type attribute to ensure the independence of the cloned object.

In PHP, goto statements are used to unconditionally jump to specific tags in the program. 1) It can simplify the processing of complex nested loops or conditional statements, but 2) Using goto may make the code difficult to understand and maintain, and 3) It is recommended to give priority to the use of structured control statements. Overall, goto should be used with caution and best practices are followed to ensure the readability and maintainability of the code.

In PHP, data statistics can be achieved by using built-in functions, custom functions, and third-party libraries. 1) Use built-in functions such as array_sum() and count() to perform basic statistics. 2) Write custom functions to calculate complex statistics such as medians. 3) Use the PHP-ML library to perform advanced statistical analysis. Through these methods, data statistics can be performed efficiently.

Yes, anonymous functions in PHP refer to functions without names. They can be passed as parameters to other functions and as return values of functions, making the code more flexible and efficient. When using anonymous functions, you need to pay attention to scope and performance issues.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
