Hướng dẫn php fibers example - ví dụ về sợi php

  • Ngày: 2021-03-08

    Nội dung chính

    • Giới thiệu
    • Đề nghị
    • Phản xạfiber
    • Thay đổi lạc hậu không tương thích
    • Phạm vi tương lai
    • Phiên bản PHP được đề xuất
    • Đề xuất lựa chọn bỏ phiếu
    • Bản vá và bài kiểm tra
    • Câu hỏi thường gặp
    • Bỏ phiếu
    • Người giới thiệu

  • Trạng thái: Thực hiện

Giới thiệu

Đề nghị

Phản xạfiber

Thay đổi lạc hậu không tương thích

Phạm vi tương lai

  • Phiên bản PHP được đề xuất

  • Đề xuất lựa chọn bỏ phiếu

  • Bản vá và bài kiểm tra

Câu hỏi thường gặp

Bỏ phiếu

  • Người giới thiệu

  • Trạng thái: Thực hiện

    Trong hầu hết lịch sử PHP, mọi người chỉ viết mã PHP dưới dạng mã đồng bộ. Thực hiện các chức năng dừng cho đến khi kết quả có sẵn để quay lại từ chức năng, bao gồm các hoạt động I/O, có thể khá chậm.
  • Gần đây, đã có nhiều dự án cho phép mọi người viết mã PHP không đồng bộ để cho phép các hoạt động I/O đồng thời. Các hàm không đồng bộ chấp nhận một cuộc gọi lại hoặc trả về trình giữ chỗ cho một giá trị trong tương lai [chẳng hạn như lời hứa] để chạy mã vào thời điểm tương lai sau khi kết quả có sẵn. Thực thi tiếp tục mà không chờ đợi một kết quả. Ví dụ về các dự án này là AMPHP, Reactphp và Guzz.

    Vấn đề mà RFC này tìm cách giải quyết là một vấn đề khó giải thích, nhưng có thể được gọi là chức năng của bạn màu gì? vấn đề.

Tóm tắt về vấn đề được mô tả trong bài viết được liên kết là:

Các hàm không đồng bộ thay đổi cách gọi hàm.

Các hàm đồng bộ có thể không gọi hàm không đồng bộ [mặc dù các hàm không đồng bộ có thể gọi các hàm đồng bộ].

Gọi một hàm không đồng bộ yêu cầu toàn bộ ngăn xếp cuộc gọi không đồng bộ

Đối với những người quen thuộc với việc sử dụng lời hứa và/hoặc chờ/năng suất để đạt được mã không đồng bộ bằng văn bản, vấn đề có thể được thể hiện là Kết quả của cuộc gọi không thể được biết đến cho đến khi lời hứa được giải quyết.

RFC này tìm cách loại bỏ sự khác biệt giữa các hàm đồng bộ và không đồng bộ bằng cách cho phép các chức năng bị gián đoạn mà không làm ô nhiễm toàn bộ ngăn xếp cuộc gọi. Điều này sẽ đạt được bằng cách:

Thêm hỗ trợ cho sợi vào PHP.

Thêm một lớp

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
0 và lớp phản xạ tương ứng
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
1.

Thêm các lớp ngoại lệ

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
2 và
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
3 để biểu thị lỗi.

Các sợi cho phép triển khai I/O không chặn trong suốt của các giao diện hiện có [như PSR-7, Doctine ORM, v.v.]. Điều này là do đối tượng giữ chỗ [lời hứa] bị loại bỏ. Thay vào đó, các chức năng có thể khai báo loại kết quả I/O thay vì đối tượng giữ chỗ không thể chỉ định loại độ phân giải vì PHP không hỗ trợ thuốc generic.

Đề nghị

Sợi

Sợi cho phép tạo ra các chức năng toàn bộ, bị gián đoạn có thể được sử dụng để thực hiện đa nhiệm hợp tác trong PHP. Chúng còn được gọi là Coroutines hoặc Green-Threads.

Sợi tạm dừng toàn bộ ngăn xếp thực thi, vì vậy người gọi trực tiếp của hàm không cần thay đổi cách gọi nó.

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
5

Việc thực thi có thể bị gián đoạn ở bất cứ đâu trong ngăn xếp cuộc gọi bằng cách sử dụng

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
4 [nghĩa là cuộc gọi đến
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
4 có thể ở trong một chức năng lồng nhau sâu hoặc thậm chí không tồn tại].

Không giống như các máy phát không có ngăn xếp, mỗi sợi có ngăn xếp cuộc gọi riêng, cho phép chúng bị tạm dừng trong các cuộc gọi chức năng được lồng sâu. Một hàm khai báo điểm gián đoạn [nghĩa là, gọi

  • final class ReflectionFiber
    {
        /**
         * @param Fiber $fiber Any Fiber object, including those that are not started or have
         *                     terminated.
         */
        public function __construct[Fiber $fiber] {}
     
        /**
         * @return Fiber The reflected Fiber object.
         */
        public function getFiber[]: Fiber {}
     
        /**
         * @return string Current file of fiber execution.
         */
        public function getExecutingFile[]: string {}
     
        /**
         * @return int Current line of fiber execution.
         */
        public function getExecutingLine[]: int {}
     
        /**
         * @param int $options Same flags as {@see debug_backtrace[]}.
         *
         * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
         *               and {@see ReflectionGenerator::getTrace[]}.
         */
        public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
     
        /**
         * @return bool True if the fiber has been started.
         */
        public function isStarted[]: bool {}
     
        /**
         * @return bool True if the fiber is currently suspended.
         */
        public function isSuspended[]: bool {}
     
        /**
         * @return bool True if the fiber is currently running.
         */
        public function isRunning[]: bool {}
     
        /**
         * @return bool True if the fiber has completed execution [either returning or
         *              throwing an exception], false otherwise.
         */
        public function isTerminated[]: bool {}
    }
    4] không cần phải thay đổi loại trả lại của nó, không giống như một hàm bằng cách sử dụng
    final class ReflectionFiber
    {
        /**
         * @param Fiber $fiber Any Fiber object, including those that are not started or have
         *                     terminated.
         */
        public function __construct[Fiber $fiber] {}
     
        /**
         * @return Fiber The reflected Fiber object.
         */
        public function getFiber[]: Fiber {}
     
        /**
         * @return string Current file of fiber execution.
         */
        public function getExecutingFile[]: string {}
     
        /**
         * @return int Current line of fiber execution.
         */
        public function getExecutingLine[]: int {}
     
        /**
         * @param int $options Same flags as {@see debug_backtrace[]}.
         *
         * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
         *               and {@see ReflectionGenerator::getTrace[]}.
         */
        public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
     
        /**
         * @return bool True if the fiber has been started.
         */
        public function isStarted[]: bool {}
     
        /**
         * @return bool True if the fiber is currently suspended.
         */
        public function isSuspended[]: bool {}
     
        /**
         * @return bool True if the fiber is currently running.
         */
        public function isRunning[]: bool {}
     
        /**
         * @return bool True if the fiber has completed execution [either returning or
         *              throwing an exception], false otherwise.
         */
        public function isTerminated[]: bool {}
    }
    7 phải trả về một thể hiện
    final class ReflectionFiber
    {
        /**
         * @param Fiber $fiber Any Fiber object, including those that are not started or have
         *                     terminated.
         */
        public function __construct[Fiber $fiber] {}
     
        /**
         * @return Fiber The reflected Fiber object.
         */
        public function getFiber[]: Fiber {}
     
        /**
         * @return string Current file of fiber execution.
         */
        public function getExecutingFile[]: string {}
     
        /**
         * @return int Current line of fiber execution.
         */
        public function getExecutingLine[]: int {}
     
        /**
         * @param int $options Same flags as {@see debug_backtrace[]}.
         *
         * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
         *               and {@see ReflectionGenerator::getTrace[]}.
         */
        public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
     
        /**
         * @return bool True if the fiber has been started.
         */
        public function isStarted[]: bool {}
     
        /**
         * @return bool True if the fiber is currently suspended.
         */
        public function isSuspended[]: bool {}
     
        /**
         * @return bool True if the fiber is currently running.
         */
        public function isRunning[]: bool {}
     
        /**
         * @return bool True if the fiber has completed execution [either returning or
         *              throwing an exception], false otherwise.
         */
        public function isTerminated[]: bool {}
    }
    8.

    Các sợi có thể được treo trong bất kỳ cuộc gọi chức năng nào, bao gồm cả các loại được gọi từ bên trong Php VM, chẳng hạn như các hàm được cung cấp cho
  • Ném một ngoại lệ từ

    final class ReflectionFiber
    {
        /**
         * @param Fiber $fiber Any Fiber object, including those that are not started or have
         *                     terminated.
         */
        public function __construct[Fiber $fiber] {}
     
        /**
         * @return Fiber The reflected Fiber object.
         */
        public function getFiber[]: Fiber {}
     
        /**
         * @return string Current file of fiber execution.
         */
        public function getExecutingFile[]: string {}
     
        /**
         * @return int Current line of fiber execution.
         */
        public function getExecutingLine[]: int {}
     
        /**
         * @param int $options Same flags as {@see debug_backtrace[]}.
         *
         * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
         *               and {@see ReflectionGenerator::getTrace[]}.
         */
        public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
     
        /**
         * @return bool True if the fiber has been started.
         */
        public function isStarted[]: bool {}
     
        /**
         * @return bool True if the fiber is currently suspended.
         */
        public function isSuspended[]: bool {}
     
        /**
         * @return bool True if the fiber is currently running.
         */
        public function isRunning[]: bool {}
     
        /**
         * @return bool True if the fiber has completed execution [either returning or
         *              throwing an exception], false otherwise.
         */
        public function isTerminated[]: bool {}
    }
    4 bằng cách sử dụng
    final class ReflectionFiber
    {
        /**
         * @param Fiber $fiber Any Fiber object, including those that are not started or have
         *                     terminated.
         */
        public function __construct[Fiber $fiber] {}
     
        /**
         * @return Fiber The reflected Fiber object.
         */
        public function getFiber[]: Fiber {}
     
        /**
         * @return string Current file of fiber execution.
         */
        public function getExecutingFile[]: string {}
     
        /**
         * @return int Current line of fiber execution.
         */
        public function getExecutingLine[]: int {}
     
        /**
         * @param int $options Same flags as {@see debug_backtrace[]}.
         *
         * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
         *               and {@see ReflectionGenerator::getTrace[]}.
         */
        public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
     
        /**
         * @return bool True if the fiber has been started.
         */
        public function isStarted[]: bool {}
     
        /**
         * @return bool True if the fiber is currently suspended.
         */
        public function isSuspended[]: bool {}
     
        /**
         * @return bool True if the fiber is currently running.
         */
        public function isRunning[]: bool {}
     
        /**
         * @return bool True if the fiber has completed execution [either returning or
         *              throwing an exception], false otherwise.
         */
        public function isTerminated[]: bool {}
    }
    03
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
32 Trả về giá trị được trả về từ một sợi bị chấm dứt [
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
33 được trả về nếu sợi không trả lại giá trị]. Hàm này sẽ ném một ví dụ là
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
2 nếu sợi chưa hoàn thành thực thi hoặc ném một ngoại lệ.
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
35 Trả về phiên bản hiện đang thực hiện
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
0 hoặc
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
33 nếu được gọi từ
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
38. Điều này cho phép một sợi lưu trữ một tham chiếu đến chính nó ở nơi khác, chẳng hạn như trong một cuộc gọi lại vòng lặp sự kiện hoặc một loạt các sợi đang chờ.

Phản xạfiber

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
1 được sử dụng để kiểm tra các sợi thực thi. Một đối tượng
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
1 có thể được tạo từ bất kỳ đối tượng
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
0 nào, ngay cả khi nó chưa được bắt đầu hoặc nếu nó đã chấm dứt. Lớp phản ánh này tương tự như
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
32.
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}

Sợi chưa hoàn thành

Các sợi chưa hoàn thành [không thực thi hoàn toàn] bị phá hủy tương tự như các máy phát điện chưa hoàn thành, thực hiện bất kỳ khối

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
33 đang chờ xử lý nào.
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
4 có thể không được gọi trong một sợi đóng, giống như
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
7 không thể được sử dụng trong một máy phát điện đóng. Sợi bị phá hủy khi không có tài liệu tham khảo về đối tượng
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
0.

Ngăn xếp sợi

Mỗi sợi được phân bổ một ngăn xếp C riêng biệt và ngăn xếp VM trên đống. Ngăn xếp C được phân bổ bằng cách sử dụng

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
37 nếu có, nghĩa là bộ nhớ vật lý chỉ được sử dụng theo yêu cầu [nếu nó cần được phân bổ cho giá trị ngăn xếp] trên hầu hết các nền tảng. Mỗi ngăn xếp sợi được phân bổ tối đa 8m bộ nhớ theo mặc định, có thể giải quyết được với cài đặt INI
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
38. Lưu ý rằng bộ nhớ này được sử dụng cho ngăn xếp C và không liên quan đến bộ nhớ có sẵn cho mã PHP. Các ngăn xếp VM cho mỗi sợi được phân bổ theo cách tương tự như máy phát và sử dụng một lượng bộ nhớ và CPU tương tự. Các ngăn xếp VM có thể phát triển động, vì vậy chỉ có một trang VM duy nhất [4K] ban đầu được phân bổ.

Thay đổi lạc hậu không tương thích

Tuyên bố

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
0,
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
2,
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
3 và
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
1 trong không gian tên gốc. Không có BC khác phá vỡ.

Phạm vi tương lai

Việc triển khai hiện tại không cung cấp API nội bộ cho các sợi cho các phần mở rộng PHP. RFC này tập trung vào API sợi không gian người dùng. Một API sợi nội bộ sẽ được thêm vào, hợp tác với các nhà phát triển nội bộ khác và sử dụng phản hồi từ các nhà phát triển mở rộng PHP, bao gồm cả swoole, vì vậy các sợi có thể được tạo và kiểm soát từ các phần mở rộng PHP. Một tiện ích mở rộng vẫn có thể tùy chọn cung cấp triển khai sợi tùy chỉnh của riêng họ, nhưng API nội bộ sẽ cho phép tiện ích mở rộng sử dụng triển khai sợi do PHP cung cấp.

Phiên bản PHP được đề xuất

Php 8.1

Đề xuất lựa chọn bỏ phiếu

Hợp nhất thực hiện thành cốt lõi, 2/3 yêu cầu.

Bản vá và bài kiểm tra

Thực hiện và kiểm tra tại AMPHP/FIBER.

AMPHP V3, một công việc đang tiến hành, sử dụng

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
53. Gần như tất cả các thư viện trong Tổ chức GitHub AMPHP đều có các nhánh tương thích với AMPHP V3. Các nhánh được dán nhãn là
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
54, trong đó
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
55 là phiên bản hiện tại + 1 [ví dụ: nhánh
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
56 của AMPHP/HTTP-client]. Xem các thư mục
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
57 trong các thư viện khác nhau để biết các mẫu mã PHP bằng cách sử dụng sợi.

React Fiber sử dụng

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
53 và các phiên bản ổn định hiện tại của
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
59 và
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
20 để tạo coroutines và chờ bất kỳ trường hợp nào của
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
21 cho đến khi nó được giải quyết.

Ví dụ

Ví dụ đơn giản đầu tiên này tạo ra một sợi ngay lập tức đình chỉ với chuỗi

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
22. Chuỗi này được trả về từ cuộc gọi đến
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
23. Sợi sau đó được nối lại với chuỗi
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
24, được trả về từ cuộc gọi đến
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
4.
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
0

Ví dụ này sẽ xuất hiện như sau:

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
1

Ví dụ tiếp theo xác định một vòng lặp sự kiện rất đơn giản với khả năng thăm dò ổ cắm cho dữ liệu đến, gọi một cuộc gọi lại khi dữ liệu có sẵn trên ổ cắm. Vòng lặp sự kiện này hiện có thể được sử dụng để tiếp tục một sợi chỉ khi dữ liệu có sẵn trên ổ cắm, tránh đọc chặn.

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
3

Tập lệnh này sẽ xuất ra như sau:

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
3

Nếu ví dụ này được viết theo thứ tự tương tự không có sợi, tập lệnh sẽ không thể đọc được từ ổ cắm trước khi ghi vào nó, vì cuộc gọi đến

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
26 sẽ chặn cho đến khi có dữ liệu.

Dưới đây là một biểu đồ minh họa luồng thực thi giữa

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
38 và sợi được tạo bởi
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
28. Chuyển đổi luồng thực thi giữa các sợi là
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
4 và
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
02 được gọi hoặc khi một sợi kết thúc.

Một vài ví dụ tiếp theo sử dụng ASYNC Framework AMPHP V3 được đề cập trong các bản vá và thử nghiệm để chứng minh cách sử dụng các sợi của các khung để tạo mã không đồng bộ được viết như mã đồng bộ.

AMPHP V3 sử dụng giao diện vòng lặp sự kiện cùng với nhiều chức năng và đối tượng giữ chỗ [

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
71] để xây dựng trên API sợi cơ bản để tạo API có ý kiến ​​của riêng mình để tạo ra các luồng màu xanh lá cây [coroutines] để thực thi mã đồng thời. Người dùng AMPHP V3 không sử dụng API sợi trực tiếp, khung xử lý việc treo và tạo sợi khi cần thiết, bao gồm thêm khả năng chờ đợi từ
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
72. Các khung khác có thể chọn cách tiếp cận việc tạo ra các chủ đề xanh và giữ chỗ khác nhau.

Hàm

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
73 tạo ra một sợi mới được thực hiện khi sợi hiện tại đình chỉ hoặc chấm dứt.
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
74 đình chỉ sợi hiện tại cho đến khi số mili giây nhất định đã giảm bớt.
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
5

Ví dụ tiếp theo một lần nữa sử dụng AMPHP V3 để chứng minh cách sợi vòng lặp sự kiện tiếp tục thực thi trong khi chủ đề chính là bị treo lơ lửng. Hàm

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
75 đình chỉ một sợi cho đến khi lời hứa đã cho được giải quyết và hàm
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
76 tạo ra một sợi mới, trả lại một lời hứa được giải quyết khi sợi hoàn thành, cho phép nhiều sợi được thực hiện đồng thời.
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
2

Vì các sợi có thể được tạm dừng trong các cuộc gọi trong PHP VM, các sợi cũng có thể được sử dụng để tạo ra các trình lặp và máy phát không đồng bộ. Ví dụ dưới đây sử dụng AMPHP V3 để treo sợi trong một máy phát, chờ phân giải

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
77, một đối tượng giống như lời hứa tự giải quyết với đối số thứ hai sau số mili giây được đưa ra là đối số đầu tiên. Khi lặp lại trên máy phát, vòng
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
00 sẽ treo trong khi chờ đợi một giá trị khác được mang lại từ máy phát.
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
7

Ví dụ dưới đây cho thấy cách Reactphp có thể sử dụng các sợi để xác định hàm

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
79 có thể được sử dụng để chờ đợi độ phân giải hứa hẹn trong một sợi sử dụng
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
90 và
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
91 của chúng.
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
9

Một minh chứng về việc tích hợp Reactphp với các sợi đã được thực hiện trong Trowski/React Fiber cho các phiên bản ổn định hiện tại của

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
59 và
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
20.

Câu hỏi thường gặp

Đối tượng mục tiêu cho tính năng này là ai?

Sợi là một tính năng nâng cao mà hầu hết người dùng sẽ không sử dụng trực tiếp. Tính năng này chủ yếu được nhắm mục tiêu vào thư viện và các tác giả khung để cung cấp một vòng lặp sự kiện và API lập trình không đồng bộ. Các sợi cho phép tích hợp thực thi mã không đồng bộ hoàn hảo vào mã đồng bộ tại bất kỳ điểm nào mà không cần phải sửa đổi ngăn xếp cuộc gọi ứng dụng hoặc thêm mã Boilerplate.

API sợi dự kiến ​​sẽ không được sử dụng trực tiếp trong mã cấp ứng dụng. Sợi cung cấp API kiểm soát dòng chảy cơ bản, cấp thấp để tạo các bản tóm tắt cấp cao hơn, sau đó được sử dụng trong mã ứng dụng.

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
94 là một ví dụ về một tính năng gần đây được thêm vào PHP mà hầu hết người dùng có thể không sử dụng trực tiếp, nhưng có thể được hưởng lợi rất nhiều trong các thư viện mà họ sử dụng.

Còn hiệu suất thì sao?

Chuyển đổi giữa các sợi là nhẹ, đòi hỏi phải thay đổi giá trị của khoảng 20 con trỏ, cho hoặc nhận, tùy thuộc vào nền tảng. Chuyển đổi ngữ cảnh thực thi trong PHP VM tương tự như các trình tạo, một lần nữa chỉ yêu cầu trao đổi một vài gợi ý. Vì các sợi tồn tại trong một luồng quy trình duy nhất, việc chuyển đổi giữa các sợi có hiệu suất cao hơn đáng kể so với chuyển đổi giữa các quá trình hoặc luồng.

Những nền tảng nào được hỗ trợ?

Sợi được hỗ trợ trên gần như tất cả các kiến ​​trúc CPU hiện đại, bao gồm x86, x86_64, 32- và 64 bit, PPC, MIPS, MIPS, Windows độc lập, Windows độc lập, Windows cung cấp API sợi] và các nền tảng POSIX cũ hơn với các nền tảng POSIX cũ hơn với uContext. Hỗ trợ chuyển đổi ngăn xếp C sử dụng mã lắp ráp được cung cấp bởi BOOST, có giấy phép được OSI chấp thuận cho phép các thành phần được phân phối trực tiếp với PHP.

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
53 được kiểm tra tích cực trên Travis cho Linux chạy trên x86_64 và cánh tay 64 bit, trên AppVeyor cho Windows và bởi các nhà phát triển trên macOS chạy trên x86_64.

Các ngăn xếp thực thi được hoán đổi như thế nào?

Mỗi sợi giữ một con trỏ đến ngăn xếp C và ngăn xếp VM [

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
96]. Khi đi vào sợi, ngăn xếp C hiện tại được hoán đổi và con trỏ
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
97 được hoán đổi cho những người được giữ bởi sợi. Ngăn xếp VM trước đó được sao lưu trong bộ nhớ trong đó việc thực thi sẽ tiếp tục khi sợi được nhập bị treo hoặc hoàn thành. Ngăn xếp VM trước đó được khôi phục trong
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
97 khi sợi trước được nhập lại, bởi sợi khác treo hoặc hoàn thành.

Các chức năng như

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
99 và ngoại lệ backtraces chỉ bao gồm dấu vết của sợi hiện tại. Các backtraces sợi trước đây hiện không được bao gồm, mặc dù điều này có thể có thể với một số sửa đổi đối với các chức năng bên trong tạo ra các dấu vết này để bao gồm cả mặt sau của các sợi đi vào sợi hiện tại.

Làm thế nào để chặn mã ảnh hưởng đến sợi

Chặn mã [như

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
00] sẽ tiếp tục chặn toàn bộ quá trình, ngay cả khi các sợi khác tồn tại. Mã phải được viết để sử dụng I/O không đồng bộ, một vòng lặp sự kiện và các sợi để xem một hiệu suất và lợi ích đồng thời. Như đã đề cập trong phần giới thiệu, một số thư viện đã tồn tại cho I/O không đồng bộ và có thể tận dụng các sợi để tích hợp với mã đồng bộ trong khi mở rộng tiềm năng đồng thời trong một ứng dụng.

Vì các sợi cho phép sử dụng I/O không đồng bộ trong suốt, việc chặn các triển khai có thể được thay thế bằng các triển khai không chặn mà không ảnh hưởng đến toàn bộ ngăn xếp cuộc gọi. Nếu một vòng lặp sự kiện nội bộ có sẵn trong tương lai, các chức năng nội bộ như

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
01 có thể được thực hiện không chặn theo mặc định.

Làm thế nào để các sợi khác nhau truy cập cùng một bộ nhớ?

Tất cả các sợi tồn tại trong một luồng duy nhất. Chỉ một sợi duy nhất có thể thực thi tại một thời điểm, do đó bộ nhớ không thể được truy cập hoặc sửa đổi đồng thời bằng nhiều sợi, không giống như các luồng có thể sửa đổi bộ nhớ đồng thời.

Khi các sợi được treo và nối lại, việc thực hiện nhiều sợi truy cập cùng một bộ nhớ có thể được xen kẽ. Do đó, một sợi chạy có thể sửa đổi bộ nhớ phụ thuộc vào một sợi lơ lửng khác. Có nhiều chiến lược khác nhau để giải quyết vấn đề này, bao gồm mutexes, semaphores, bưu kiện bộ nhớ và kênh. RFC này không cung cấp bất kỳ triển khai nào như vậy vì chúng có thể được thực hiện trong mã không gian người dùng bằng API sợi được đề xuất.

Tại sao thêm điều này vào Core PHP?

Thêm khả năng này trực tiếp trong php Core làm cho nó có sẵn rộng rãi trên bất kỳ máy chủ nào cung cấp PHP. Thông thường người dùng không thể xác định phần mở rộng nào có thể có sẵn trong một môi trường lưu trữ cụ thể, không chắc chắn cách cài đặt các tiện ích mở rộng hoặc không muốn cài đặt các phần mở rộng của bên thứ 3. Với các sợi trong php Core, bất kỳ tác giả thư viện nào cũng có thể sử dụng tính năng mà không cần lo ngại về tính di động.

Các tiện ích mở rộng mà mã hồ sơ cần tính đến việc chuyển đổi sợi khi tạo backtraces và tính toán thời gian thực thi. Điều này cần được cung cấp như một API nội bộ cốt lõi để bất kỳ trình hồ sơ nào cũng có thể hỗ trợ các sợi. API nội bộ sẽ được cung cấp nằm ngoài phạm vi của RFC này vì nó sẽ không ảnh hưởng đến mã người dùng.

Tại sao không thêm một vòng lặp sự kiện và API ASYNC/AIDIT vào Core?

RFC này chỉ đề xuất tối thiểu trần cần thiết để cho phép mã người dùng triển khai các coroutines hoặc luồng xanh đầy đủ trong PHP. Có một số khung thực hiện API vòng lặp sự kiện của riêng họ, lời hứa và các API không đồng bộ khác. Các API này khác nhau rất nhiều và được thiết kế, được thiết kế cho một mục đích cụ thể và nhu cầu cụ thể của chúng có thể không thể được API cốt lõi thiết kế chỉ được thiết kế bởi một vài cá nhân.

Ý kiến ​​của các tác giả của RFC này là tốt nhất là cung cấp mức tối thiểu trong lõi và cho phép mã người dùng thực hiện các thành phần khác như họ mong muốn. Nếu cộng đồng chuyển sang một API vòng lặp sự kiện hoặc nhu cầu xuất hiện cho một vòng lặp sự kiện trong php Core, điều này có thể được thực hiện trong RFC trong tương lai. Cung cấp một vòng lặp sự kiện cốt lõi mà không có chức năng cốt lõi bằng cách sử dụng nó [như luồng, truy cập tệp, v.v.] sẽ gây hiểu lầm và khó hiểu cho người dùng. Trì hoãn chức năng như vậy cho các khung người dùng và chỉ cung cấp API tối thiểu trong Core giữ cho các kỳ vọng trong kiểm tra.

RFC này không loại trừ việc thêm async/chờ đợi và một vòng lặp sự kiện vào lõi.

Làm thế nào để đề xuất này khác với các đề xuất sợi trước?

RFC sợi trước không hỗ trợ chuyển đổi ngữ cảnh trong các cuộc gọi nội bộ [

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
9,
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
03, v.v.] hoặc trình xử lý Opcode [
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
00,
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
05, v.v.]. Điều này có thể dẫn đến sự cố nếu một hàm sử dụng sợi được sử dụng trong bất kỳ mã người dùng nào được gọi từ mã C hoặc trong các tiện ích mở rộng ghi đè
final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
06 như XDebug.

Các sợi có tương thích với các phần mở rộng, bao gồm cả XDebug?

Các sợi không thay đổi cách PHP VM thực thi mã PHP và đình chỉ được hỗ trợ trong ngăn xếp C, do đó, các sợi tương thích với các tiện ích mở rộng PHP chỉ đơn giản là cung cấp một cầu nối cho API C, bao gồm cả những người sử dụng các cuộc gọi lại có thể gọi

final class ReflectionFiber
{
    /**
     * @param Fiber $fiber Any Fiber object, including those that are not started or have
     *                     terminated.
     */
    public function __construct[Fiber $fiber] {}
 
    /**
     * @return Fiber The reflected Fiber object.
     */
    public function getFiber[]: Fiber {}
 
    /**
     * @return string Current file of fiber execution.
     */
    public function getExecutingFile[]: string {}
 
    /**
     * @return int Current line of fiber execution.
     */
    public function getExecutingLine[]: int {}
 
    /**
     * @param int $options Same flags as {@see debug_backtrace[]}.
     *
     * @return array Fiber backtrace, similar to {@see debug_backtrace[]}
     *               and {@see ReflectionGenerator::getTrace[]}.
     */
    public function getTrace[int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT]: array {}
 
    /**
     * @return bool True if the fiber has been started.
     */
    public function isStarted[]: bool {}
 
    /**
     * @return bool True if the fiber is currently suspended.
     */
    public function isSuspended[]: bool {}
 
    /**
     * @return bool True if the fiber is currently running.
     */
    public function isRunning[]: bool {}
 
    /**
     * @return bool True if the fiber has completed execution [either returning or
     *              throwing an exception], false otherwise.
     */
    public function isTerminated[]: bool {}
}
4.

Một số tiện ích mở rộng nối vào VM PHP và do đó được đặc biệt quan tâm cho khả năng tương thích.

  • XDebug tương thích như một lỗi trong phiên bản 3.0.1. Các điểm dừng có thể được đặt trong các sợi và được kiểm tra như bình thường trong các IDE và gỡ lỗi như Phpstorm. Bảo hiểm mã hoạt động như mong đợi.

  • PCOV tạo phạm vi bảo hiểm mã như mong đợi, bao gồm cả mã được thực thi trong các sợi riêng biệt.

  • Song song có thể sử dụng sợi trong các luồng.

Như đã lưu ý trong tại sao thêm điều này vào php Core ?, Các tiện ích mở rộng mã hồ sơ, tạo backtraces, cung cấp thời gian thực thi, v.v. sẽ cần được cập nhật để giải thích cho việc chuyển đổi giữa các sợi để cung cấp dữ liệu chính xác.

Bỏ phiếu

Bỏ phiếu bắt đầu vào ngày 2021-03-08 và sẽ diễn ra trong suốt 2021-03-22. 2/3 yêu cầu chấp nhận.

Người giới thiệu

Bài Viết Liên Quan

Chủ Đề