Am currently learning swoole's coroutine, the characteristic of the coroutine is that it appears to be executed concurrently. Experiment with it.
Recommended for free: swoole
The premise of this article is that swoole has been installed on Linux. The latest version is 4.5.9 (2020-12-17)
Construct a requirement, create 5 coroutines at once, each coroutine sleeps a random number between 1 and 3 seconds, and then prints out For sleep time, we hope that when all programs end, the entire process takes up to 3 seconds.
1.php
foreach (range(1, 5) as $v) { go(function () { $sleep_time = random_int(1, 3); sleep($sleep_time); echo "睡眠了" . $sleep_time . "秒\n"; });}
Using php 1.php to execute, the program can be executed, but it does not feel like a coroutine, but is executed synchronously. The entire execution takes far more than 3 seconds. It turns out there is a small bug here. You should use co:sleep, so that you can sleep within this coroutine without affecting the entire program.
2.php
foreach (range(1, 5) as $v) { go(function () { $sleep_time = random_int(1, 3); co::sleep($sleep_time); echo "睡眠了" . $sleep_time . "秒\n"; });}
The results are as follows:
睡眠了1秒 睡眠了2秒 睡眠了2秒 睡眠了3秒 睡眠了3秒
In short, the coroutine that sleeps less must exit first, and the coroutine that sleeps for the same time prints out at the same time. character. And the total time taken at the end of the program is 3 seconds, indicating that the concurrency is successful.
Now I hope that after the 5 coroutines end, hello world can be printed!
3.php
foreach (range(1, 5) as $v) { go(function () { $sleep_time = random_int(1, 3); co::sleep($sleep_time); $a = random_int(1, 1000); echo "睡眠了" . $sleep_time . "秒\n"; });}echo "hello world!\n";
The result is another problem, hello world is always printed first.
So, you need to use the coroutine container here.
4.php
Co\run(function () { foreach (range(1, 5) as $v) { go(function () { $sleep_time = random_int(1, 3); co::sleep($sleep_time); echo "睡眠了" . $sleep_time . "秒\n"; }); }});echo "hello world!\n";
The results are as follows:
睡眠了1秒 睡眠了2秒 睡眠了3秒 睡眠了3秒 睡眠了3秒 hello world!
The correct result can be printed this time, perfect!
swoole’s coroutine is still a bit interesting~
The above is the detailed content of A preliminary exploration of swoole coroutine. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)
