{"id":18320661,"url":"https://github.com/xtlsoft/xcoroutine","last_synced_at":"2026-03-08T16:04:57.289Z","repository":{"id":57085107,"uuid":"100332484","full_name":"xtlsoft/XCoroutine","owner":"xtlsoft","description":"XCoroutine, the PHP Coroutine.","archived":false,"fork":false,"pushed_at":"2017-08-17T00:36:07.000Z","size":9,"stargazers_count":7,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-21T13:12:45.503Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xtlsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-15T03:01:15.000Z","updated_at":"2023-05-21T15:41:59.000Z","dependencies_parsed_at":"2022-08-25T00:50:16.046Z","dependency_job_id":null,"html_url":"https://github.com/xtlsoft/XCoroutine","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtlsoft%2FXCoroutine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtlsoft%2FXCoroutine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtlsoft%2FXCoroutine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtlsoft%2FXCoroutine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xtlsoft","download_url":"https://codeload.github.com/xtlsoft/XCoroutine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411238,"owners_count":20934650,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T18:17:03.453Z","updated_at":"2026-03-08T16:04:57.243Z","avatar_url":"https://github.com/xtlsoft.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# XCoroutine\nXCoroutine, the PHP Coroutine.\n## install 安装\n```sh\ncomposer require xtlsoft/xcoroutine\n```\n## Intro 介绍\n- Task: 任务实例。您可以向里面传入一个Generator，然后通过我们的封装对他进行操作。\n- Scheduler: 多任务执行封装。您可以往里面add许多Task，然后执行。每个Task有唯一的ID，一个Task进行部分计算调用yield后，让下一个Task进行计算，以此循环，实现多任务管理。\n- SystemCall: Task之间和Task与Scheduler之间传递数据、互相操作的接口。\n- DynamicObject: 动态编程类，让你的类支持动态定义/销毁变量和方法。\n\n## Usage 使用\n1. 初识yield\n    例子：\n    ```php\n    \u003c?php\n        \n        foreach(range(1,10) as $i){\n            echo $i.\" \";\n        }\n        echo \"\u003cbr\u003e\\n\";\n        \n        function xrange($a,$b,$c=1){\n            for($i = $a; $a \u003c= $b; $i+=$c){\n                yield $i;\n            }\n        }\n        \n        foreach(xrange(1,10) as $i){\n            echo $i.\" \";\n        }\n        \n    ```\n    输出：\n    ```html\n    1 2 3 4 5 6 7 8 9 10\n    1 2 3 4 5 6 7 8 9 10\n    ```\n2. Task 使用\n    例子：\n    ```php\n    \u003c?php\n        require \"vendor/autoload.php\";\n        use Coroutine\\Task;\n        \n        function TaskExample(){\n            while(true){\n                $text = (yield);\n                echo \"TaskRecv $text \u003cbr /\u003e\\n\";\n            }\n        }\n        \n        $Task = new Task(TaskExample());\n        $Task-\u003erun();\n        $Task-\u003erun(\"A Test Text 1\");\n        $Task-\u003esetSendValue(\"Another Test Text\");\n        $Task-\u003erun();\n        \n    ```\n    输出：\n    ```html\n    TaskRecv A Test Text 1 \n    TaskRecv Another Test Text \n    ```\n3. Scheduler 使用\n    例子#1：\n    ```php\n    \u003c?php\n        require_once \"vendor/autoload.php\";\n        use Coroutine\\Scheduler;\n        \n        function TaskExample($num){\n            for($i=0; $i\u003c10; ++$i){\n                echo \"Task$num RUN $i \u003cbr\u003e\\n\";\n                yield;\n            }\n        }\n        \n        $scheduler = new Scheduler();\n        $scheduler-\u003enewTask(TaskExample(1));\n        $scheduler-\u003enewTask(TaskExample(2));\n        $scheduler-\u003enewTask(TaskExample(3));\n        \n        $scheduler-\u003erun();\n    ```\n    例子#2:\n    ```php\n    \u003c?php\n        require_once \"vendor/autoload.php\";\n        use Coroutine\\Scheduler;\n        \n        function t1(){\n            for($i=0; $i\u003c10; ++$i){\n                echo \"t1 RUN $i \u003cbr\u003e\\n\";\n                yield;\n            }\n        }\n        function t2(){\n            for($i=0; $i\u003c10; ++$i){\n                echo \"t2 RUN $i \u003cbr\u003e\\n\";\n                yield;\n            }\n        }\n        \n        $scheduler = new Scheduler();\n        $scheduler-\u003enewTask(t1());\n        $scheduler-\u003enewTask(t2());\n        \n        $scheduler-\u003erun();\n    ```\n4. SystemCall 使用\n    例子：\n    ```php\n    \u003c?php\n        require_once \"vendor/autoload.php\";\n        use Coroutine\\Scheduler;\n        use Coroutine\\SystemCall;\n        \n        function TaskExample($num){\n            //获取TaskId\n            $tid = (yield SystemCall::getTaskId());\n            \n            for($i=0; $i\u003c10; ++$i){\n                echo \"Task$num $tid RUN $i \u003cbr\u003e\\n\";\n                if($i == 3){\n                    yield SystemCall::killTask(1);\n                }\n                if($i == 5){\n                    yield SystemCall::newTask(Task2());\n                }\n                yield;\n            }\n        }\n        \n        function Task2(){\n            $tid = (yield SystemCall::getTaskId());\n            for($i=0; $i\u003c3; ++$i){\n                echo \"Another Task $tid RUN $i \u003cbr\u003e\\n\";\n            }\n        }\n        \n        $scheduler = new Scheduler();\n        $scheduler-\u003enewTask(TaskExample(1));\n        $scheduler-\u003enewTask(TaskExample(2));\n        $scheduler-\u003enewTask(TaskExample(3));\n        \n        $scheduler-\u003erun();\n    ```\n5. DynamicObject 使用\n    例子：\n    ```php\n    \u003c?php\n        require(\"vendor/autoload.php\");\n        use Coroutine\\DynamicObject;\n        \n        $obj = new DynamicObject;\n        \n        $obj-\u003eabcd = 123;\n        \n        $obj-\u003emyFunc = function($a) use ($obj){\n            return (\"\\$a * abcd = \".($a * $obj-\u003eabcd).\"\u003cbr /\u003e\\n\");\n        };\n        \n        echo $obj-\u003emyFunc(2);\n        \n        unset($obj-\u003eabcd);\n        \n        echo $obj-\u003emyFunc(1);\n        \n        $obj-\u003emyFunc = function($a, $b){\n            return (\"\\$a + \\$b = \".($a+$b).\"\u003cbr /\u003e\\n\");\n        };\n        \n        $obj-\u003emyFunc(123,321);\n        \n    ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtlsoft%2Fxcoroutine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxtlsoft%2Fxcoroutine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtlsoft%2Fxcoroutine/lists"}