Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of PHP Co-programming and blocking

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "PHP collaboration and congestion example Analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Processes, threads, collaborators

There are very detailed and rich blogs or learning resources about processes, threads, and collaborations. I will not repeat them here. I will briefly introduce these things here.

The process has its own independent heap and stack, neither sharing the heap nor sharing the stack, and the process is scheduled by the operating system.

Threads have their own independent stack and shared heap, shared heap, not shared stack, and threads are scheduled by the operating system (standard threads are).

The co-program shares the heap like the thread, not the stack, and the programmer displays the schedule in the code of the co-program.

Implementing the basic yield of the Cooperative Program in PHP

The fundamental implementation of yield is the generator class, and the iterator class is the implementation of the iterator interface:

Generator implements Iterator {public mixed current (void) / / returns the currently generated value public mixed key (void) / / returns the currently generated key public void next (void) / / the generator continues to execute the public void rewind (void) / / reset iterator, where an exception is thrown if the iteration has already begun. / / the execution of renwind will cause * yield to be executed and its return value ignored. Public mixed send (mixed $value) / / passes a value to the generator as the result of the yield expression, and then continues to execute the generator. If the generator / / is not in the yield expression when this method is called, it will run to * yield expressions before passing in the value. Public void throw (Exception $exception) / / throws an exception public bool valid (void) / / checks whether the iterator is turned off public void _ _ wakeup (void) / / serializes the callback, and throws an exception to indicate that the generator cannot be serialized. }

Custom simple timed execution task example:

(this example must rely on the cooperative scheduling code implemented by Bird above)

Class timer {private $start = 0; / timing start time private $timer; / / time difference between intervals in seconds private $value = 0; / / the resulting value private $callback; / / Asynchronous callback private $isEnd = false; / / whether the current timer task ends public function _ _ construct ($timer,callable $callback) {$this- > start = time (); $this- > timer = $timer $this- > callback = $callback;} public function run () {if ($this- > valid ()) {$callback = $this- > callback; $callback ($this- > value + +, $this); $this- > start = time ();}} / * * regular check * / public function valid () {$end = time () If ($end-$this- > start > = $this- > timer) {return true;} else {return false;}} public function setEnd ($isEnd) {$this- > isEnd = $isEnd;} public function getEnd () {return $this- > isEnd }} / function taskObject1 () {$timer = new timer (1 value,timer $timer) {if ($value > = 5) {$timer- > setEnd (true);} echo'

'. A'. $value;}); $tid = (yield getTaskId ()); while (true) {if ($timer- > getEnd () = = true) {break;} yield $timer- > run () }} / function taskObject2 () {$timer = new timer (2 value,timer $timer) {if ($value > = 3) {$timer- > setEnd (true);} echo'

'.' B'. $value;}); $tid = (yield getTaskId ()); while (true) {if ($timer- > getEnd () = = true) {break;} yield $timer- > run ();} $scheduler = new Scheduler; $scheduler- > newTask (taskObject1 ()); $scheduler- > newTask (taskObject2 ()); $scheduler- > run ()

The above implementation is as follows:

Generate two tasks, execute them in parallel, and block each task for a few seconds when it is executed

So that the cooperative program can be switched smoothly, and the task blocking does not affect each other.

Think about:

Why should I do the above thing? Because I found that although the implementation of the cooperative program is very powerful and interesting, it can make the multi-task parallel, but when I call the system function sleep () in one of the tasks, the blocking task will prevent the cooperative program from switching. In fact, the same is true in terms of the principle of the implementation of the cooperative program.

In that case, I also want to simulate cooperative blocking, but do not produce blocking to see if it is feasible. PHP itself only provides a generator to provide support for co-program calls, if it does not rely on extension, does not provide multi-threaded program implementation, is not as powerful as java, and can be implemented with sub-threads.

I have the impression that the child threads of java execute independently and do not block each other, so I wonder if PHP can implement a mechanism similar to multithreading, so can it be non-blocking during calls?

After such an implementation and thinking, we fell into a misunderstanding at the beginning, which was caused by the blocking of the PHP native function sleep (), that is, to truly achieve non-blocking or asynchronous words, we must rely on the bottom of the language.

Later, I figured out a truth: since a method or function will cause blocking during execution, why not just change the current method to a custom one and make it non-blocking (as opposed to the whole co-scheduling)? For example, I have implemented one of the above timed execution.

On the other hand, the purpose of cooperative scheduling itself is to cut the task execution process into as small pieces as possible, so as to quickly switch execution and achieve the purpose of parallel. From this point of view, the cooperative process should also be regarded as a kind of programming idea.

The following is an example of a program that is executed in as small pieces as possible:

/ / A simple example

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report