Swoole the timer in the server does not have the accurate jitter situation, asks the answer

Source: Internet
Author: User
the log of the timer print appears jitter, as follows:
Timer: # #23496 # #2016-09-01 19:23:24
Timer: # #23496 # #2016-09-01 19:23:25
Timer: # #23496 # #2016-09-01 19:23:27
Timer: # #23496 # #2016-09-01 19:23:27
Timer: # #23496 # #2016-09-01 19:23:28
Timer: # #23496 # #2016-09-01 19:23:29
Timer: # #23496 # #2016-09-01 19:23:30
Timer: # #23496 # #2016-09-01 19:23:31
Timer: # #23496 # #2016-09-01 19:23:33
Timer: # #23496 # #2016-09-01 19:23:33
Timer: # #23496 # #2016-09-01 19:23:36
Timer: # #23496 # #2016-09-01 19:23:37
Timer: # #23496 # #2016-09-01 19:23:39
Timer: # #23496 # #2016-09-01 19:23:39
Timer: # #23496 # #2016-09-01 19:23:40
Timer: # #23496 # #2016-09-01 19:23:41
Timer: # #23496 # #2016-09-01 19:23:42
Timer: # #23496 # #2016-09-01 19:23:43
Timer: # #23496 # #2016-09-01 19:23:45

Is there a problem with the process signal registration in the timer? Or is there a problem with signal and server?
The code is as follows:


  Exit (1);        }); if (! (            $pid = $process->start ())) {} box:: $list [$pid] = Array ("Start" = Microtime (True),    "Process" = $process,);        } static function Registersingal () {swoole_process::signal (SIGTERM, function ($signo) {exit ();        });                Swoole_process::signal (SIGCHLD, function ($signo) {while ($ret = Swoole_process::wait (false)) {                $pidx = $ret [' pid '];                Print_r ($ret);                Print_r (self:: $list);                    if (Isset (self:: $list [$pidx])) {echo "SIGNAL: $signo \ n";                    $end = Microtime (true);                    $start = Box:: $list [$pidx] [start]; Echo ("##{$pidx} exit ... [Runtime:.] sprintf ("%0.6f", $end-$start). "]").                    Php_eol;                unset (Box:: $list [$pidx]); } else {echo "noNo SIGNAL: $signo \ n ";    }            }        }); }}#################################################################################################### $http = New Swoole_http_server ("0.0.0.0", 9501,swoole_base);//Initialize Swoole service $http->set (Array (' task_worker_num ' = 2, ' wo  Rker_num ' + 2, ' daemonize ' + 0,//whether as a daemon, this configuration generally with log_file use ' max_request ' and ' Dispatch_mode ' = 2, ' Debug_mode ' + 1, ' log_file ' = '/swoole.log ', '); $http->on (' Start ', function () {echo ' Start pid# '. Posix_getpid (). '#' . __line__.        Php_eol; Echo Swoole_version. "Onstart\n";} ); $http->on (' Workerstart ', function ($serv, $worker _id) use ($http) {//BIND timer When worker process is turned on swoole_set_p        Rocess_name (' Lau_php_worker '. Posix_getpid ());            Add a timer only if worker_id is 0 o'clock, avoid adding if ($worker _id = = 0) {box::registersingal (); Swoole_timer_tick (1000,function ($params) {box::p rocesSDo ();        }); }); $http->on (' request ', function ($request, $response) use ($http) {echo ' ____request pid# '. Posix_getpid (). '#' . __line__.    Php_eol;    $out = Json_encode (box:: $list); $response->end ($out);}); $http->on (' Task ', function ($http, $taskId, $fromId, $request) {echo ' +++++task: '. $http->worker_pid.    Php_eol; return;}); $server = $http; $http->on (' Finish ', function ($server, $taskId, $ret) {}); Swoole_set_process_name (' Lau_php_main '). Posix_getpid ()); $http->start ();

Reply content:

The log of the timer print appears jitter, as follows:
Timer: # #23496 # #2016-09-01 19:23:24
Timer: # #23496 # #2016-09-01 19:23:25
Timer: # #23496 # #2016-09-01 19:23:27
Timer: # #23496 # #2016-09-01 19:23:27
Timer: # #23496 # #2016-09-01 19:23:28
Timer: # #23496 # #2016-09-01 19:23:29
Timer: # #23496 # #2016-09-01 19:23:30
Timer: # #23496 # #2016-09-01 19:23:31
Timer: # #23496 # #2016-09-01 19:23:33
Timer: # #23496 # #2016-09-01 19:23:33
Timer: # #23496 # #2016-09-01 19:23:36
Timer: # #23496 # #2016-09-01 19:23:37
Timer: # #23496 # #2016-09-01 19:23:39
Timer: # #23496 # #2016-09-01 19:23:39
Timer: # #23496 # #2016-09-01 19:23:40
Timer: # #23496 # #2016-09-01 19:23:41
Timer: # #23496 # #2016-09-01 19:23:42
Timer: # #23496 # #2016-09-01 19:23:43
Timer: # #23496 # #2016-09-01 19:23:45

Is there a problem with the process signal registration in the timer? Or is there a problem with signal and server?
The code is as follows:


  Exit (1);        }); if (! (            $pid = $process->start ())) {} box:: $list [$pid] = Array ("Start" = Microtime (True),    "Process" = $process,);        } static function Registersingal () {swoole_process::signal (SIGTERM, function ($signo) {exit ();        });                Swoole_process::signal (SIGCHLD, function ($signo) {while ($ret = Swoole_process::wait (false)) {                $pidx = $ret [' pid '];                Print_r ($ret);                Print_r (self:: $list);                    if (Isset (self:: $list [$pidx])) {echo "SIGNAL: $signo \ n";                    $end = Microtime (true);                    $start = Box:: $list [$pidx] [start]; Echo ("##{$pidx} exit ... [Runtime:.] sprintf ("%0.6f", $end-$start). "]").                    Php_eol;                unset (Box:: $list [$pidx]); } else {echo "noNo SIGNAL: $signo \ n ";    }            }        }); }}#################################################################################################### $http = New Swoole_http_server ("0.0.0.0", 9501,swoole_base);//Initialize Swoole service $http->set (Array (' task_worker_num ' = 2, ' wo  Rker_num ' + 2, ' daemonize ' + 0,//whether as a daemon, this configuration generally with log_file use ' max_request ' and ' Dispatch_mode ' = 2, ' Debug_mode ' + 1, ' log_file ' = '/swoole.log ', '); $http->on (' Start ', function () {echo ' Start pid# '. Posix_getpid (). '#' . __line__.        Php_eol; Echo Swoole_version. "Onstart\n";} ); $http->on (' Workerstart ', function ($serv, $worker _id) use ($http) {//BIND timer When worker process is turned on swoole_set_p        Rocess_name (' Lau_php_worker '. Posix_getpid ());            Add a timer only if worker_id is 0 o'clock, avoid adding if ($worker _id = = 0) {box::registersingal (); Swoole_timer_tick (1000,function ($params) {box::p rocesSDo ();        }); }); $http->on (' request ', function ($request, $response) use ($http) {echo ' ____request pid# '. Posix_getpid (). '#' . __line__.    Php_eol;    $out = Json_encode (box:: $list); $response->end ($out);}); $http->on (' Task ', function ($http, $taskId, $fromId, $request) {echo ' +++++task: '. $http->worker_pid.    Php_eol; return;}); $server = $http; $http->on (' Finish ', function ($server, $taskId, $ret) {}); Swoole_set_process_name (' Lau_php_main '). Posix_getpid ()); $http->start ();

You this is normal phenomenon, you can turn on the computer clock settings to see if the number of seconds is 4 seconds short 5 seconds long rule? specifically see below

A while ago, the squirrel will be mentioned in the Windows system of the second hand to go up, the first four seconds fast, and then a second slow. And then it started this year, heralding the upheaval of the earth, and so on. I'm not impressed. However, this fast and slow phenomenon is true, and now someone has provided an explanation, is the problem of the procedure:

If you pay careful attention, open the clock in the lower-right corner of the 2K/XP/2K3 system, and let his second hand move around, there will be a very interesting phenomenon: the first four seconds the number and the second seconds change fast, five seconds feeling is very long, this is why?

The reason is actually on the accuracy of that table.

If you learn about C + + programming in Windows, Wm_timer is a common timer. But the accuracy of this timer is not very high. In the Windows clock This subroutine, is actually uses the wm_timer to carry on the timing. When the second hand is displayed, the update of the table is triggered by such a statement:

SetTimer (HWnd, Timerid, Open_tlen, 0L);

The use of SetTimer is simple, after the timer is set, each trigger will just have to re-draw the current time to redraw the dial. Then Open_tlen is the timer's trigger time. This time is a constant that is defined in the affected system as 450, which is less than half a second.

What happens when you define 450ms? Let's look at a group of numbers. The first and third lines are the number of updates, and they are the number of milliseconds to update.

1 2 3 4 5 6 7 8
450 900 1350 1800 2250 2700 3150 3600

9 10 11 12 13 14 15 16 17
4050 4500 4950 5400 5850 6300 6750 7200 7650

As you can see, the table in the first second (<1000) has actually been updated two times, second, third, and four seconds. And in the first five seconds, you need to wait three updates to see (note that 9, 10, 11 are 4xxx). So there is this strange phenomenon: the first four times are a number of two will jump a second, the fifth time to count three to jump a second, the first five seconds of the appearance of more than four seconds are 50%, so it feels very long.

Then why did you design it like this? It's probably because the watch is just a matter of time, and if you don't go wrong, the second-level response isn't important ... After all, not many people in the world stare at the second hand for a lot of beats.

The above explanations are from opal.

  • 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.