Overturn common sense-dead loop structure code, also has the opportunity to execute the logic outside the loop

Source: Internet
Author: User
Tags zookeeper client
Disruption to common sense-the code of the endless loop structure also has the opportunity to execute the logic outside the loop & lt ;? Php & nbsp; class & nbsp; ZookeeperDemo & nbsp; extends & nbsp; Zookeeper & nbsp ;{& nbsp; & nbsp; public knowledge-dead loop structure code, it also has the opportunity to execute non-circular logic
 
Class ZookeeperDemo extends Zookeeper {
 
Public function watcher ($ I, $ type, $ key ){
Echo "Insider Watcher \ n ";
 
// Watcher gets consumed so we need to set a new one
$ This-> get ('/test', array ($ this, 'Watcher '));
}
 
}
 
$ Zoo = new ZookeeperDemo ('2017. 0.0.1: 100 ');
$ Zoo-> get ('/test', array ($ zoo, 'Watcher '));
 
While (true ){
Echo '.';
Sleep (2 );
}


Run the script now.

$ Php zookeeperdemo1.php
A vertex is generated every 2 seconds. Switch to the ZooKeeper client and update the "/test" value.

[Zk: 127.0.0.1: 2181 (CONNECTED) 20] set/test foo
This will silently trigger the "Insider Watcher" message in the PHP script.


How can this happen? Why can I execute the watcher method after entering the while structure. Please read the official discussion.


Share:
------ Solution --------------------
Distributed applications?
ZooKeeper is a distributed, open-source distributed application coordination service that contains a simple primitive set and is an important component of Hadoop and Hbase. Currently, Java and C interfaces are provided.

It is understandable that you say it is a common sense of disruption.
------ Solution --------------------
Watcher gets consumed so we need to set a new one
You should be more aware of this than I do.

Php Zookeeper is always listening for php behavior
------ Solution --------------------
Isn't it normal that the subthread continues to execute tasks in an endless loop of the main thread?
------ Solution --------------------
There is a pcntl in php that can fork multiple processes for execution.

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.