Disruption to common sense-code with an endless loop structure, and the opportunity to execute logic outside the loop-php Tutorial

Source: Internet
Author: User
Tags zookeeper client
Disruption to common sense-code with an endless loop structure, and the opportunity to execute logic outside the loop
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.



Reply to discussion (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.

Instead of distributed, distributed is just what my business needs.

What's about disruption is --

In PHP, the structure of the endless loop has the opportunity to execute the logic that is obviously unrelated to the Loop-this is the performance of multithreading-so we guess this zookeeper extension runs in the php environment in a multi-thread way.

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

I am just planning to do this.

I mean, isn't it strange if someone writes and debugs a piece of php code and finds that there is a chance to execute code that is completely irrelevant to the loop in the endless loop process?

Isn't it normal that the subthread continues to execute tasks in an endless loop of the main thread?

There is a pcntl in php that can fork multiple processes for execution.

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