Asynchronous programming-How does PHP execute code asynchronously?

Source: Internet
Author: User
For example, to record some log data, I do not need real-time storage, I want to execute the code immediately after the execution of the code, I do not need callback processing, if the data sent to the message queue, it will take time, I would like to continue the following code in the millisecond, as to who he sent the data, and how much time I do not , as an example,
Echo 1;
log (data);
Echo 2;
I want the log function to execute in the background, as for how much time I don't care, but I don't have to wait for him, for me it is the millisecond level of execution.

Reply content:

For example, to record some log data, I do not need real-time storage, I want to execute the code immediately after the execution of the code, I do not need callback processing, if the data sent to the message queue, it will take time, I would like to continue the following code in the millisecond, as to who he sent the data, and how much time I do not , as an example,
Echo 1;
log (data);
Echo 2;
I want the log function to execute in the background, as for how much time I don't care, but I don't have to wait for him, for me it is the millisecond level of execution.

Send Message Queue VS millisecond level >>> where is the contradiction point?

Redis can be a queue, Redis operations read and write tens of thousands of times per second, you put the message into Redis inside also 1 write operation bar, time consumption is less than milliseconds.
So I think the use of the queue, you can fully achieve the millisecond level of demand it.

In addition, regardless of the means by which to perform asynchronous operations, always with time overhead, inevitably.

//程序被阻塞10秒shell_exec('timeout 10 vmstat 1 >/dev/null 2>&1 &');//程序不会被阻塞pclose(popen('timeout 10 vmstat 1 >/dev/null 2>&1 &', 'r'));//因此可以异步执行任务pclose(popen("timeout 60 php /path/to/task.php '$arg' >/dev/null 2>&1 &", 'r'));

Where the variable $arg is passed to the script task.php parameters, task.php through $argv[1] to get this parameter.
Timeout 60 means that the maximum execution time for the task.php script is 60 seconds, which can be removed if not required.
The essence of Pclose (Popen ()) Implementation of asynchronous is to open a process to execute the blocking code,
For asynchronous scenarios that do not require automatic return of results (callbacks) when execution is complete.

The string parameter $arg can be enclosed in single quotation marks, avoiding the effects of some spaces, but still flawed.
String arguments are best or serialize serialized to a file,
And then give the script task.php file path this parameter,
Let task.php read the file itself unserialize deserialize the data.
The file name should be unique, such as a user ID or process PID plus a time random number:

$filename = md5(uniqid($uid.'_',        true));$filename = md5(uniqid($getmypid().'_', true));
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.