Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112811 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58669 invoked from network); 8 Jan 2021 09:27:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Jan 2021 09:27:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5A07D180503 for ; Fri, 8 Jan 2021 01:04:10 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 8 Jan 2021 01:04:09 -0800 (PST) Received: by mail-il1-f182.google.com with SMTP id w17so9548329ilj.8 for ; Fri, 08 Jan 2021 01:04:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=/2kKU5ABDMUm4QtTgl9VcxFnYGjHaXV8KyCpoB3zviY=; b=S8pOQlLvYKCuxMSxjudh2BbtLJGc7FfXKKxyE2SzTqF1aejwEzR+ZxkzBZoKoSOCUV Am9NOs3r6mL7MuEqpdTtRN4nxe+ydHAPxNrGT+0vYByQRKGmaU+uk4bcoXiugi4osxdi 2PnbCYlUSoWdYwZaViyCaXSkkfHPE5Lnd3eN+y9PifcFthou5QpK00zDE50Npbis7v9D 5EFiIJJ2GnBbQ2d0factDjf82l1M0pMTuv/HJPJFClTHd9Q0QQTVz+YXSNIQMNNYdJLL qWl5rJVwuolEwTbgK4ktXP+/pYwj3yOHfh9ZtZhvE2L1prvgr9BAJSrM6hFRKWBxkmiR /4AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=/2kKU5ABDMUm4QtTgl9VcxFnYGjHaXV8KyCpoB3zviY=; b=kvKN13ihcq0K/ZGzyFwWj9oAVavgvBucnDoa92DQ6LDxVpddsfjTaBSvURfsWGhwix wWYVNTi6PyoNEI5R40UdFJ4IGrSOerOHxXnNPzEtY1Kwa78wdAehGhC0N/DwrbgEZxNC NlUpdzg/qr2+XgOBIqho3n0HtutrGQN9IB63zC75PAwp1vOL7eIFQnyoMUpdIthaF2m9 S5zQDgKq/c4rXFPo8uBzoDCwjduiF+6tLi9l1Vh+2at93RJQ3wV4GytZxuNMybEY84pK 30InfT1Y/wnMGYIh7pDjPZHFEFqWnGmNkGRvNlVZ4d3XDKGlfwkyE0GaYHO5q5CLL0Qz IWDg== X-Gm-Message-State: AOAM5306A12veD/qvaCJLg8S7eqxjGDsRJxHTlONvVNgJ38BzIUz3GxZ kFS7NZCIQ6p4t9HxmgA/x5DrGsCl2F5jqFFy6p0= X-Google-Smtp-Source: ABdhPJxv3BvmqBQ0q/OG0v0p4RCymlxVIXNXViEy1MJauT3ec6xP8JE+0dH+H9X6HtPjFCx+CmnTEsPAA5gZA1kK9Gk= X-Received: by 2002:a92:d303:: with SMTP id x3mr2938700ila.38.1610096647376; Fri, 08 Jan 2021 01:04:07 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 8 Jan 2021 01:03:56 -0800 Message-ID: To: Aaron Piotrowski Cc: php internals Content-Type: multipart/alternative; boundary="000000000000b8932b05b85fd6f0" Subject: Re: [PHP-DEV] [RFC] Fibers From: sarkedev@gmail.com (Peter Stalman) --000000000000b8932b05b85fd6f0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi again Aaron, I've been playing around with this for a bit, and I have some more questions. I see you recently changed how the `FiberScheduler` works, making it `final` and adding some "is" functions to match it more to a regular fiber. Since the `FiberScheduler` is itself also a fiber, why does it need to be a separate class? Why did you choose to go this way and make it "special" instead of not just making a regular fiber into a scheduler in userland. Could you have two or more schedulers? Could you get by without a scheduler and call a fiber inside a fiber recursively? Related to that, I noticed it was not possible to call `Fiber::this()` from within the scheduler, why is that? Is it not just another fiber? Or is this to prevent it from being passed to another scheduler? Alternatively, going the other way, instead of making the scheduler a unique class that needs to be passed around, why not go the more "traditional" PHP route and pattern it after `register_shutdown_function(callable $callback, mixed ...$args) : void`, `spl_autoload_register(callable $autoload_function =3D ?, bool $throw =3D t= rue, bool $prepend =3D false) : bool`, and those type of functions? After all, isn't it just a callback too? Something like `register_fiber_scheduler(callable $callback) : void`? This would remove the need for a special scheduler class and the need for passing the scheduler back to the `Fiber::suspend()`. Each `suspend()` call would bubble up through the registered scheduler callbacks. This would allow competing schedulers to work nicer together, instead of one scheduler having to finish before the higher up scheduler can run it's next loop. Either way, doesn't the fiber already know which scheduler it is in when it suspends? I think this would go along with simplifying it and keep the implementation broad to allow for various userland implementations (as mentioned, such as amphp and reactphp). But it probably doesn't simplify the C implementation... Anyways, something like this? $fiber =3D Fiber::this(); $callbacks =3D [ fn() =3D> $fiber->resume("Test"), ]; register_fiber_scheduler(function() use ($callbacks) { foreach ($callbacks as $callback) { $callback(); } }); $value =3D Fiber::suspend(); echo "After resuming main fiber: ", $value, "\n"; // Output: After resuming main fiber: Test Btw, this is just me vomiting my thoughts, I don't know enough about fibers to design it one way or the other, but I hope to understand it more as well as give a few different perspectives on it. Thanks, Peter On Thu, Dec 17, 2020 at 8:30 AM Aaron Piotrowski wrote: > Hello everyone! > > I would like to introduce an RFC for adding full-stack fibers to PHP: > https://wiki.php.net/rfc/fibers > > Fibers are primarily used to implement green-threads or coroutines for > asynchronous I/O. Fibers are similar to threads, except fibers exist with= in > a single thread and require cooperative scheduling of the fibers by the > process. Since fibers do not require a full CPU context switch, they are > lightweight and more performant than multi-processing or threading for > awaiting I/O. > > An implementation as an extension is at https://github.com/amphp/ext-fibe= r > > Fibers are a complex feature. The RFC contains many examples and links to > code using fibers to help explain and demonstrate what is possible, howev= er > I=E2=80=99m certain many more questions and concerns will arise. Looking = forward to > feedback and discussion. > > Aaron Piotrowski > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --000000000000b8932b05b85fd6f0--