모든 사람이 일반적으로 작성하는 for 루프를 예로 들어 보겠습니다. go처럼
두 개를 작성하고 각각에 루프를 작성하여 동시에 입력하는 것을 볼 수 있습니다. 이전 버전의 PHP에서는 하나의 cli만 열리고 여러 개의 for 루프가 작성된 경우 해당 출력은 순차적이어야 합니다. 교차 출력은 달성할 수 없습니다(즉, 첫 번째 루프에서 여러 번 실행한 후 b를 다시 실행할 수 있고 일정 기간 동안 b를 실행한 다음 A를 실행할 수 있음).
이제 섬유의 도움으로 이 작업도 수행할 수 있습니다. [추천 학습: go
PHP 비디오 튜토리얼
다음 코드는 두 루프의 교차 실행을 달성할 수 있습니다. 두 프로그램의 실행 빈도도 제어할 수 있습니다(예: A는 3번 실행되고 B는 한 번 실행됨).
<?php $t1 = false; $t2 = false; $reg = []; $reg[] = new \Fiber(function () use (&$t1) { for ($i = 1; $i < 10; $i++) { echo $i; echo PHP_EOL; \Fiber::suspend(); } $t1 = true; }); $reg[] = new \Fiber(function () use (&$t2) { for ($i = 1; $i < 10; $i++) { echo $i; echo PHP_EOL; \Fiber::suspend(); } $t2 = true; }); $startTag = true; while (count($reg) > 1) { if ($startTag) foreach ($reg as $pI) { $pI->start(); $startTag = false; } foreach ($reg as $pI) { $pI->resume(); } if ($t1 === true && $t2 === true) { break; } }
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9두 루프의 실행 빈도도 제어할 수 있습니다. 예를 들어 첫 번째 루프가 3번 실행된 후입니다. , 두 번째 루프는 한 번 실행됩니다. 코드는 다음과 같습니다
<?php $reg = []; $fId = 1; $reg[$fId] = new \Fiber(function () use (&$reg, $fId) { for ($i = 1; $i < 10; $i++) { echo $fId . ':' . $i; echo PHP_EOL; if ($i % 3 == 0) { \Fiber::suspend(); } } unset($reg[$fId]); }); $fId++; $reg[$fId] = new \Fiber(function () use (&$reg, $fId) { for ($i = 1; $i < 10; $i++) { echo $fId . ':' . $i; echo PHP_EOL; \Fiber::suspend(); } unset($reg[$fId]); }); $startTag = true; while (count($reg) > 0) { if ($startTag) foreach ($reg as $pI) { $pI->start(); $startTag = false; } foreach ($reg as $pI) { $pI->resume(); } }
1:1 1:2 1:3 2:1 1:4 1:5 1:6 2:2 1:7 1:8 1:9 2:3 2:4 2:5 2:6 2:7 2:8 2:9메시지 알림으로 완료
<?php namespace App\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; #[AsCommand( name: 'Sname', description: 'Add a short description for your command', )] class SnameCommand extends Command { protected function configure(): void { $this ->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description') ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description'); } protected function execute(InputInterface $input, OutputInterface $output): int { $t1 = false; $t2 = false; $reg = []; $fId = 1; $reg[] = new \Fiber(function () use ($fId) { for ($i = 1; $i < 10; $i++) { echo $fId . ':' . $i; echo PHP_EOL; if ($i % 3 == 0) { \Fiber::suspend(new SuspendData(Status::Running)); } } \Fiber::suspend(new SuspendData(Status::Stop)); }); $fId++; $reg[] = new \Fiber(function () use ($fId) { for ($i = 1; $i < 10; $i++) { echo $fId . ':' . $i; echo PHP_EOL; \Fiber::suspend(new SuspendData(Status::Running)); } \Fiber::suspend(new SuspendData(Status::Stop)); }); $startTag = true; while (count($reg) > 0) { if ($startTag) foreach ($reg as $pI) { $pI->start(); $startTag = false; } foreach ($reg as $key => $pI) { $r = $pI->resume(); if ($r->status === Status::Stop) { unset($reg[$key]); } } } return Command::SUCCESS; } } class SuspendData { public readonly Status $status; public function __construct($status) { $this->status = $status; } } enum Status { case Stop; case Running; }
위 내용은 PHP8.1 Fiber는 멀티 태스킹을 교차 실행합니다(자세한 코드 설명 포함)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!
성명
이 기사는 learnku에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
어 ass 신 크리드 그림자 : 조개 수수께끼 솔루션
2 몇 주 전ByDDD
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경
