Skip to content

Whether Coroutine\Channel and Coroutine\Defer are supported #1

Closed

Description

@tw2066

Activity

  1. EdmondDantes commented on Jun 23, 2025

    @EdmondDantes
    Contributor

    Good afternoon. Of course, channels are planned to be supported.
    defer is already supported as the onFinally() method.
    https://github.com/true-async/php-async/blob/main/tests/coroutine/014-coroutine_onFinally_basic.phpt

  2. self-assigned this
    on Jun 23, 2025
  3. added
    question
    on Jun 23, 2025
  4. tw2066 commented on Jun 24, 2025

    @tw2066
    Author

    Good morning!
    I appreciate your prompt response.
    onFinally() method is not run in the swpan() method, and it is not very easy to release resources;

    use function Async\spawn;
    use function Async\await;
    use function Async\suspend;
    
    $called = false;
    
    $coroutine = spawn(
        function () {
            $dbh = new PDO($dsn = '', $user = '', $pass = '');
    
            // defer(fn()=>$dbh = null);
    
    
            //foreach ($dbh->query('SELECT * from FOO') as $row) {
            //    print_r($row);
            //}
    
            return "test result";
        }
    );
    
    $coroutine->onFinally(
        function () use (&$called) {
            $called = true;
            echo "Finally callback executed\n";
        }
    );
    
    await($coroutine);
    
    // Give finally handler time to execute
    suspend();
  5. EdmondDantes commented on Jun 24, 2025

    @EdmondDantes
    Contributor

    onFinally() method is not run in the swpan() method, and it is not very easy to release resources;

    Isn't it? This method can be used anywhere.
    like: currentCoroutine()->onFinally().
    Although I haven’t written such a test yet, it’s possible that not everything works :)

    We could also create an Async\onFinally function to avoid writing such a long expression:

    $coroutine = spawn(
        function () {
            $dbh = new PDO($dsn = '', $user = '', $pass = '');
    
            onFinally(fn()=>$dbh = null);

    But there's a deeper issue here.
    Currently, onFinally handlers are executed in a separate coroutine, not in the one where they were registered. This is done to improve code safety and prevent finally handlers from blocking coroutine termination.

    On the other hand, it would be useful to be able to execute code at the end of a function without switching to another coroutine. In this sense, onFinally is not the same.

    I would prefer to add a new syntactic construct to PHP that directly implements this scenario.

    However, the new syntactic construct is not limited to asynchronous code — it applies to PHP as a whole. Therefore, for now, onFinally handlers are a way to achieve the same goal.

  6. tw2066 commented on Jun 24, 2025

    @tw2066
    Author

    Thank you for your answer.

  7. EdmondDantes commented on Jun 24, 2025

    @EdmondDantes
    Contributor

    Thank you for the helpful questions!

  8. added a commit that references this issue on Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

question

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions