Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126582 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 706201A00BC for ; Wed, 5 Mar 2025 17:50:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1741196894; bh=h18l8aOUCTZbADcCqLPsOhmFz/FOMpi3wqhqPIO3tVc=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=bNTusrJU8GVsrAAuiCXZT/9f9QMeOH+uMxT9338S60BA5F7unqsa0kD96MQKBrxK8 i4utHKi/jB2uee44IPlTZxO/HmAOhdeLqsEVNhDgyx9FFD9nb+VvmapUBHzVHUTfy1 Tr8DL8nYK5zZaE5Tw/KJBfTq+Gw80N3uXtskSsFdWHpX1m/V1VVtpvCjTVfV3Jlme2 R0UofT3cooI6bi1QEoJ53IZm97gMwbB6E+6PiBo/ItOjmcVoHfo5djX1kUquwjyMif 5Cc4h1uM691WNWNxAf6otKONLHrX7m36/Lr7JuO1XKPDuIiXWTA7t95eHs50ozguZq CMK2gNPD5gQIQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 75815180068 for ; Wed, 5 Mar 2025 17:48:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.173]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 5 Mar 2025 17:48:13 +0000 (UTC) Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-6f768e9be1aso12459807b3.0 for ; Wed, 05 Mar 2025 09:50:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741197048; x=1741801848; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=/G4ZPRcjkgG56+HCEHvP0gjkDGYIEHwfW/HQRFdfVA8=; b=JKdmJIAYHJMPUc7yoBsTZ7xOoTeqNBw+ORHg97SNUBbiM9UTHjuqAK5R5f2QE3JuBg PEK/R6J9h7WfaOju3M4V2dtAjIuOZHu1ahr7U2hDl6/+Eyq/RK1rwT5qKwR6b4CX/0qe xCtqPXDZO62Cs1teewCwIbuxY3GgBx3ItC201pfIi2xNVp1qwnA/SyorN8UOeUR1p/Kh +HjOwy3gASVYuWb7sRZDj3+mV99Om55LiwKWCu7omSPeZgY7/4dBEMwGlb1q7gtiftXN M1F31ChebszWgB3MNyNdb05TmNuT2gv9ZrKcmt/wUrcSTiRiQ42IGF3yxFOYf0x8Vc4a vhxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741197048; x=1741801848; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/G4ZPRcjkgG56+HCEHvP0gjkDGYIEHwfW/HQRFdfVA8=; b=pAo5jOjM4UKKNpU2w/Olbq5Bz3aLIE6qaMxINwSZzjEVVhTbfIeeVnyyXlH/zWyhrj Z96B224aYpLsTCMj8xjpmAkCDGF0RDNV5spdGCZ7DNlyGeylYPbTn1rqF8SbTvJ3JxT7 3uml5NNufkTM6byk75U0jJY0iFmSxFqIFheNJs+w7GG0OQcROIW9c2dvGoNGMGkZkooI BF7g6Y8HkDqmSEhzFSbz0JVYhsrzuu/77M0nf9akABYl4gi8slMAUO43ExeBQ1yz7tkn V2nKXu2qNZiaxbVnjastLXV4xtfPCz/VdBPJYoOJNguyLcguaeD01pQlab0Omy/MFh4G sqHg== X-Gm-Message-State: AOJu0YyUFzHlKD5hQv24nwCGobqEv1wx4tJ5IQ4hPcYz9/bgNIzG9CQ6 u9+okQRL4Ei+Q2HydA4HsYEUb8+6BRetgS3bg2dhWGAWKpA1obttLMnL/HIO91R0EO3gevm/xie aqea4eKGBcQT9lgeqQmU28oZJCtTJzjSyb2I= X-Gm-Gg: ASbGnctn6rpH99kU18Z1DHIjm35zDfcQkEON+XCA7gkfARuJK752srUAwTFJOZVN4Gh 27rmsENqEjbSKuJvs6CxUpGCQXKzLEWrepZpXilTanqGnFpyKMAPGua9fqb0ZfCupMP8u9iLu18 wcN1JVlQArd5uBsA1xvyQkVZm8Cg== X-Google-Smtp-Source: AGHT+IGFI9ZFstGiRWpwWabMoF+euscQyQHYR1YwqLWDllOepBQ5PLQMO+x5slLI9x369+JG6us4uhy7/2ZggD2aR1Y= X-Received: by 2002:a05:690c:4e05:b0:6fe:a868:597 with SMTP id 00721157ae682-6feaef245b5mr2967337b3.14.1741197048211; Wed, 05 Mar 2025 09:50:48 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 5 Mar 2025 19:50:37 +0200 X-Gm-Features: AQ5f1JpJvuqSWRZjQPZPJUqcDEPQbEcj1vH7NANKHSZhPJNYxgAExlYj63yJIhQ Message-ID: Subject: Re: [PHP-DEV] PHP True Async RFC To: Jakub Zelenka Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000008af43a062f9c079b" From: edmond.ht@gmail.com (Edmond Dantes) --0000000000008af43a062f9c079b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > You might want to look to parallel extension as it's already dealing > with that and mostly works - of course combination with coroutines will certainly complicate it but the point is that memory is not shared. Do you mean this extension: https://www.php.net/manual/en/book.parallel.php= ? Yes, I studied it before starting the development of True Async. My very first goal was to enable, if not coroutine execution across threads, then at least interaction, because the same thing is already possible with Swoole. However, parallel does not provide multitasking and will not be able to in the future. The best it can offer is interaction via Channel between two different threads, which will be useful for job processing and built-in web servers. And here's the frustrating part. It turns out that parallel has to copy PHP bytecode for correct execution in another thread. This means that not only does the memory manager need to be replaced with a multi-threaded version, but the virtual machine itself must also be refactored. For PHP to work correctly in multiple threads with context switching, it will be necessary to find a way to rewrite all the code that reads/writes global variables. (Where TLS macros are used, this shouldn't be too difficult. But is that the case everywhere?) This applies to all extensions, both built-in and third-party. Such a language update would create an "extension vacuum." When a new version is released, many extensions will become unavailable due to the need to adapt to the new multitasking model. > I didn't really mean to introduce it as part of this RFC. > What I meant is to design the API so there is still possibility to add it in the future without risking various race condition in the code. > It means primarily to put certain restrictions that will prevent it like limited access to global and passing anything by > reference (including objects) to the running tasks. Primitives like *Context* are unlikely to be the main issue for multitasking. The main problem will be the code that has been developed for many years with single-threaded execution in mind. This is another factor that raises doubts about the rationale for introducing real multitasking in PHP. If we are talking about a model similar to Python=E2=80=99s, the current RF= C already works with it, as a separate thread is used on Windows to wait for processes and send events to the PHP thread. Therefore, integrating this RFC with parallel is not an issue. It would be great to solve the bytecode problem in a way that allows it to be freely executed across different threads. This would enable running any closure as a coroutine in another OS thread and interacting through a channel. If you talk about this functionality, it does not block concurrency or current RFC. This capability should be considered as an additional feature that can be implemented later without modifying the existing primitives. While working on this RFC, I also considered finding a way to create SharedObject instances that could be passed between threads. However, I ultimately concluded that this solution would require changes to the memory manager, so these objects were not included in the final document. Ed. --0000000000008af43a062f9c079b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
>=C2=A0 You might want to look to parallel extension as it's already dealing > with that and mostly works - of course combination with coroutines w= ill certainly complicate it but the point is that memory is not shared.

Do you mean this extension: https://www.php.net/manual/en/book.parallel= .php?

Yes, I studied it before starting the development of True A= sync. My very first goal was to enable, if not coroutine execution across t= hreads, then at least interaction, because the same thing is already possib= le with Swoole.

However, parallel does not provide multi= tasking and will not be able to in the future. The best it can offer is int= eraction via Channel between two different threads, which will= be useful for job processing and built-in web servers.

And here'= s the frustrating part. It turns out that parallel has to copy= PHP bytecode for correct execution in another thread. This means that not = only does the memory manager need to be replaced with a multi-threaded vers= ion, but the virtual machine itself must also be refactored.

For PHP = to work correctly in multiple threads with context switching, it will be ne= cessary to find a way to rewrite all the code that reads/writes global vari= ables. (Where TLS macros are used, this shouldn't be too difficult. But= is that the case everywhere?) This applies to all extensions, both built-i= n and third-party.

Such a language update would create an "exten= sion vacuum." When a new version is released, many extensions will bec= ome unavailable due to the need to adapt to the new multitasking model.

=

> I didn't really mean to introduce it as part of this RFC.
&g= t; What I meant is to design the API so there is still possibility to add i= t in the future without risking various race condition in the code.
>= ; It means primarily to put certain restrictions that will prevent it like = limited access to global and passing anything by
> reference (includ= ing objects) to the running tasks.

Primitives like Context are unlikely to be the main issue for multitasking. The main problem= will be the code that has been developed for many years with single-thread= ed execution in mind. This is another factor that raises doubts about the r= ationale for introducing real multitasking in PHP.

If we are talking = about a model similar to Python=E2=80=99s, the current RFC already works wi= th it, as a separate thread is used on Windows to wait for processes and se= nd events to the PHP thread. Therefore, integrating this RFC with par= allel is not an issue.

It would be great to solve the bytecode= problem in a way that allows it to be freely executed across different thr= eads. This would enable running any closure as a coroutine in another OS th= read and interacting through a channel. If you talk about this=C2=A0 functi= onality, it does not block concurrency or current RFC.=C2=A0 This capability should be considered as an additional feature that can be i= mplemented later without modifying the existing primitives.

While working on this RFC, I also considered finding a way to create= SharedObject instances that could be passed between threads. = However, I ultimately concluded that this solution would require changes to= the memory manager, so these objects were not included in the final docume= nt.

Ed.

--0000000000008af43a062f9c079b--