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

Source: Internet
Author: User
Tags zookeeper client
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 (' 127.0.0.1:2181 ');
$zoo->get ('/test ', Array ($zoo, ' Watcher '));

while (true) {
echo '. ';
Sleep (2);
}


Now run the script.

$ PHP zookeeperdemo1.php
This should produce a point every 2 seconds. Now switch to the Zookeeper client and update the "/test" value.

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


How did that happen? Why the Watcher method can be executed after entering the while structure. Please crossing to discuss the issue.



Reply to discussion (solution)

Distributed applications?
Zookeeper is a distributed, open-source, distributed application Coordination service that contains a simple set of primitives and is an important component of Hadoop and HBase. Currently, Java and C interfaces are available.

It's understandable, you say, to subvert common sense.

It's not about the distribution, it's just my business needs.

The place of subversion is--

In PHP, the structure of the dead loop has the opportunity to perform a loop-independent logic outside of the loop--this is the performance of multithreading--and we suspect that the zookeeper extension is running in a multithreaded manner in a PHP environment.

Watcher gets consumed so we need to set a new one
You should know more about this than I do.

PHP Zookeeper is always listening to PHP behavior

I'm not doing this, I'm just going to do this.

I mean, wouldn't it be strange to have someone write debugging a PHP code and find that there is a chance to execute code that doesn't really matter to the loop in the process of a dead loop?

Is it not normal for a child thread to continue to perform a task in the main thread's dead loop?

There's a pcntl in PHP that can fork out a multi-process execution.

Definitely not fork.

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