{"id":34605213,"url":"https://github.com/true-async/php-async","last_synced_at":"2025-12-24T13:39:28.984Z","repository":{"id":295242052,"uuid":"970630806","full_name":"true-async/php-async","owner":"true-async","description":"PHP Async extension","archived":false,"fork":false,"pushed_at":"2025-12-17T08:49:48.000Z","size":1419,"stargazers_count":171,"open_issues_count":13,"forks_count":2,"subscribers_count":15,"default_branch":"main","last_synced_at":"2025-12-18T19:58:24.993Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/true-async.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-04-22T09:52:07.000Z","updated_at":"2025-12-18T08:00:31.000Z","dependencies_parsed_at":"2025-06-30T21:27:27.090Z","dependency_job_id":"79093239-3d4a-49fa-b668-f959dd5fee4c","html_url":"https://github.com/true-async/php-async","commit_stats":null,"previous_names":["true-async/php-async"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/true-async/php-async","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/true-async%2Fphp-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/true-async%2Fphp-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/true-async%2Fphp-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/true-async%2Fphp-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/true-async","download_url":"https://codeload.github.com/true-async/php-async/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/true-async%2Fphp-async/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28003382,"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","status":"online","status_checked_at":"2025-12-24T02:00:07.193Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2025-12-24T13:39:28.081Z","updated_at":"2025-12-24T13:39:28.977Z","avatar_url":"https://github.com/true-async.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# \u003cimg src=\"true-async-logo.png\" alt=\"PHP TRUE ASYNC Logo\" height=\"48\" style=\"vertical-align: middle; margin-right: 12px;\" /\u003e PHP TRUE ASYNC\n\n**PHP TRUE ASYNC** is an experimental `PHP` extension that provides a true asynchronous, \ntightly integrated at the core level.\n\n---\n\n## Table of Contents\n\n- [Features](#features)\n- [Installation](#installation)\n    - [Unix / macOS](#unix--macos)\n    - [Windows](#windows)\n- [Adapted PHP Functions](#adapted-php-functions)\n- [Quick Start](#quick-start)\n- [Documentation](#documentation)\n- [Contributing](#contributing)\n- [License](#license)\n- [Links](#links)\n\n---\n\n## Features\n\n*Coming soon!*\n\n---\n\n## Installation\n\nPHP TRUE ASYNC is supported for PHP 8.5.0 and later.\n`LibUV` is the primary reactor implementation for this extension.\n\n### Docker (for tests)\n\n```bash\n# Build the image\ndocker build -t true-async-php .\n\n# Run interactively\ndocker run -it true-async-php bash\n\n# Check TrueAsync module\ndocker run --rm true-async-php php -m | grep true_async\n```\n\n### Requirements\n\n- **PHP 8.5.0+**\n- **LibUV ≥ 1.45.0** (required) - Fixes critical `UV_RUN_ONCE` busy loop issue that could cause high CPU usage\n\n### Why LibUV 1.45.0+ is Required\n\nPrior to libuv 1.44, there was a critical issue in `uv__io_poll()`/`uv__run_pending` logic that could cause the event loop to \"stick\" after the first callback when running in `UV_RUN_ONCE` mode, especially when new ready events appeared within callbacks. This resulted in:\n\n- **High CPU usage** due to busy loops\n- **Performance degradation** in async applications\n- **Inconsistent event loop behavior** affecting TrueAsync API reliability\n\nThe fix in libuv 1.44 ensures that `UV_RUN_ONCE` properly returns after processing all ready callbacks in the current iteration, meeting the \"forward progress\" specification requirements. This is essential for TrueAsync's performance and reliability.\n\n---\n\n### Unix / macOS\n\n1. **Clone the PHP repository:**\n\n    for example, basic directory name is `php-src`:\n\n   ```\n   git clone https://github.com/true-async/php-src -b true-async-api ./php-src\n   ```\n\n2. **Clone the `True Async` extension repository:**\n\n    to the `ext` directory of your PHP source:\n\n    ```\n    git clone https://github.com/true-async/php-async ./php-src/ext/async\n    ```\n\n3. **Install PHP development tools:**\n\n    Make sure you have the necessary development tools installed. On Debian/Ubuntu, you can run:\n    \n    ```\n    sudo apt-get install php-dev build-essential autoconf libtool pkg-config\n    ```\n    \n    For macOS, you can use Homebrew:\n    \n    ```\n    brew install autoconf automake libtool pkg-config\n    ```\n\n4. **Install LibUV:**:\n   \n**IMPORTANT:** LibUV version 1.45.0 or later is required.\n\nFor Debian/Ubuntu:\n```bash\n# Check if system libuv meets requirements (≥1.45.0)\npkg-config --modversion libuv\n\n# If version is too old, install from source:\nwget https://github.com/libuv/libuv/archive/v1.45.0.tar.gz\ntar -xzf v1.45.0.tar.gz\ncd libuv-1.45.0\nmkdir build \u0026\u0026 cd build\ncmake .. -DCMAKE_BUILD_TYPE=Release\nmake -j$(nproc)\nsudo make install\nsudo ldconfig\n```\n\nFor macOS:\n```bash\n# Homebrew usually has recent versions\nbrew install libuv\n```\n\nPlease see the [LibUV installation guide](https://github.com/libuv/libuv) for more details.\n\n5. **Configure and build:**\n\n   ```\n   ./buildconf\n   ./configure --enable-async\n   make \u0026\u0026 sudo make install\n   ```\n\n   We can use `--enable-debug` to enable debug mode, which is useful for development.\n   \n   **Note:** The `--enable-experimental-async-api` option is no longer needed as the Async API is now enabled by default in the core.\n\n---\n\n### Windows\n\n1. **Install php-sdk:**  \n   Download and set up [php-sdk](https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2) for building PHP extensions on Windows.\n\n2. **Install and build LibUV:**  \n   You can use [vcpkg](https://github.com/microsoft/vcpkg) or build libuv from source.\n\n3. **Copy LibUV files to PHP SDK directories:**\n\n   ```\n   1. Copy everything from 'libuv\\include' to '%PHP_SDK_PATH%\\deps\\include\\libuv\\'\n   2. Copy 'libuv.lib' to '%PHP_SDK_PATH%\\deps\\lib\\'\n   ```\n   `%PHP_SDK_PATH%` is your php-sdk installation root.\n\n4. **Configure and build the extension with PHP:**\n\n   ```\n   cd \\path\\to\\php-src\n   buildconf\n   configure --enable-async\n   nmake\n   ```\n\n   **Note:** The `--enable-experimental-async-api` option is no longer needed as the Async API is now enabled by default in the core.\n\n---\n\n## Adapted PHP Functions\n\n**50+ PHP functions** have been adapted to work asynchronously when used within coroutines:\n\n### DNS Functions\n- `gethostbyname()` - resolve hostname to IP address\n- `gethostbyaddr()` - resolve IP address to hostname  \n- `gethostbynamel()` - get list of IP addresses for hostname\n\n### Database Functions\n- **PDO MySQL** - async-compatible PDO operations\n  - `PDO::__construct()`, `PDO::prepare()`, `PDO::exec()` - non-blocking\n  - `PDOStatement::execute()`, `PDOStatement::fetch()` - async data access\n- **MySQLi** - async-compatible MySQLi operations  \n  - `mysqli_connect()`, `mysqli_query()`, `mysqli_prepare()` - non-blocking\n  - `mysqli_stmt_execute()`, `mysqli_fetch_*()` - async result fetching\n\n### CURL Functions  \n- `curl_exec()` - execute cURL request\n- `curl_multi_exec()` - execute multiple cURL handles\n- `curl_multi_select()` - wait for activity on multiple cURL handles\n- `curl_multi_getcontent()` - get content from multi handle\n- `curl_setopt()`, `curl_getinfo()`, `curl_error()`, `curl_close()` - async-aware\n\n### Socket Functions\n- `socket_connect()`, `socket_accept()` - connection operations\n- `socket_read()`, `socket_write()` - data transfer\n- `socket_send()`, `socket_recv()` - data exchange\n- `socket_sendto()`, `socket_recvfrom()` - addressed data transfer\n- `socket_bind()`, `socket_listen()` - server operations\n- `socket_select()` - monitor socket activity\n\n### Stream Functions\n- `file_get_contents()` - get file/URL contents\n- `fread()`, `fwrite()` - file I/O operations\n- `fopen()`, `fclose()` - file handle management\n- `stream_socket_client()`, `stream_socket_server()` - socket streams\n- `stream_socket_accept()` - accept stream connections\n- `stream_select()` - monitor stream activity\n- `stream_context_create()` - async-aware context creation\n\n### Process Execution Functions\n- `proc_open()` - open process with pipes\n- `exec()` - execute external command\n- `shell_exec()` - execute shell command\n- `system()` - execute system command  \n- `passthru()` - execute and pass output directly\n\n### Sleep/Timer Functions\n- `sleep()` - delay execution (seconds)\n- `usleep()` - delay execution (microseconds)\n- `time_nanosleep()` - nanosecond precision delay\n- `time_sleep_until()` - sleep until timestamp\n\n### Output Buffer Functions\n- `ob_start()` - start output buffering with coroutine isolation\n- `ob_flush()`, `ob_clean()` - buffer operations with isolation\n- `ob_get_contents()`, `ob_end_clean()` - get/end buffer with isolation\n\nAll functions automatically become non-blocking when used in async context, allowing other coroutines to continue execution while waiting for I/O operations to complete.\n\n---\n\n## Quick Start\n\n### Basic Coroutine Example\n\n```php\n\u003c?php\n\n// Spawn multiple concurrent coroutines\nAsync\\spawn(function() {\n    echo \"Starting coroutine 1\\n\";\n    sleep(2); // Non-blocking in async context\n    echo \"Coroutine 1 completed\\n\";\n});\n\nAsync\\spawn(function() {\n    echo \"Starting coroutine 2\\n\";\n    sleep(1); // Non-blocking in async context\n    echo \"Coroutine 2 completed\\n\";\n});\n\necho \"All coroutines started\\n\";\n```\n\n### Concurrent DNS Lookups\n\n```php\n\u003c?php\n\n$start = microtime(true);\n\n// Start multiple DNS lookups concurrently\nAsync\\spawn(function() {\n    $ip = gethostbyname('github.com'); // Non-blocking\n    $ips = gethostbynamel('github.com'); // Get all IPs\n    echo \"GitHub: $ip (\" . count($ips) . \" total IPs)\\n\";\n});\n\nAsync\\spawn(function() {\n    $ip = gethostbyname('google.com'); // Non-blocking\n    $hostname = gethostbyaddr($ip); // Reverse lookup\n    echo \"Google: $ip -\u003e $hostname\\n\";\n});\n\nAsync\\spawn(function() {\n    $content = file_get_contents('http://httpbin.org/ip'); // Non-blocking\n    echo \"External IP: \" . json_decode($content)-\u003eorigin . \"\\n\";\n});\n\n$elapsed = microtime(true) - $start;\necho \"All operations completed in: \" . round($elapsed, 3) . \"s\\n\";\n```\n\n### Concurrent HTTP Requests with CURL\n\n```php\n\u003c?php\n\n$urls = [\n    'https://httpbin.org/delay/1',\n    'https://httpbin.org/delay/2',\n    'https://httpbin.org/delay/1'\n];\n\n$start = microtime(true);\n\nforeach ($urls as $i =\u003e $url) {\n    Async\\spawn(function() use ($url, $i) {\n        $ch = curl_init($url);\n        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n        curl_setopt($ch, CURLOPT_TIMEOUT, 10);\n        \n        $response = curl_exec($ch); // Non-blocking\n        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n        curl_close($ch);\n        \n        echo \"Request $i: HTTP $httpCode\\n\";\n    });\n}\n\n$elapsed = microtime(true) - $start;\necho \"All requests completed in: \" . round($elapsed, 3) . \"s\\n\";\n```\n\n### Async Database Operations\n\n```php\n\u003c?php\n\n// Concurrent database queries with PDO MySQL\nAsync\\spawn(function() {\n    $pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);\n    \n    // All operations are non-blocking in async context\n    $stmt = $pdo-\u003eprepare(\"SELECT * FROM users WHERE active = ?\");\n    $stmt-\u003eexecute([1]);\n    \n    while ($row = $stmt-\u003efetch(PDO::FETCH_ASSOC)) {\n        echo \"User: {$row['name']}\\n\";\n    }\n});\n\n// MySQLi concurrent operations\nAsync\\spawn(function() {\n    $mysqli = new mysqli('localhost', $user, $pass, 'test');\n    \n    // Non-blocking query execution\n    $result = $mysqli-\u003equery(\"SELECT COUNT(*) as total FROM orders\");\n    $row = $result-\u003efetch_assoc();\n    echo \"Total orders: {$row['total']}\\n\";\n    \n    $mysqli-\u003eclose();\n});\n\necho \"Database queries started\\n\";\n```\n\n### Process Execution\n\n```php\n\u003c?php\n\n// Execute multiple commands concurrently\nAsync\\spawn(function() {\n    $output = shell_exec('sleep 2 \u0026\u0026 echo \"Command 1 done\"'); // Non-blocking\n    echo $output;\n});\n\nAsync\\spawn(function() {\n    $output = shell_exec('sleep 1 \u0026\u0026 echo \"Command 2 done\"'); // Non-blocking\n    echo $output;\n});\n\necho \"Commands started\\n\";\n```\n\n### Output Buffering with Coroutine Isolation\n\n```php\n\u003c?php\n\n// Each coroutine has isolated output buffer\nAsync\\spawn(function() {\n    ob_start(); // Isolated buffer\n    echo \"Output from coroutine 1\\n\";\n    echo \"More output from coroutine 1\\n\";\n    $buffer1 = ob_get_contents();\n    ob_end_clean();\n    \n    echo \"Coroutine 1 captured: $buffer1\";\n});\n\nAsync\\spawn(function() {\n    ob_start(); // Separate isolated buffer\n    echo \"Output from coroutine 2\\n\";\n    $buffer2 = ob_get_contents();\n    ob_end_clean();\n    \n    echo \"Coroutine 2 captured: $buffer2\";\n});\n\necho \"Buffers are isolated between coroutines\\n\";\n```\n\n---\n\n## Documentation\n\n- [Documentation (coming soon)](https://github.com/true-async/php-async-docs)\n\n---\n\n## Contributing\n\nPull requests and suggestions are welcome!  \nPlease read [CONTRIBUTING.md](CONTRIBUTING.md) before starting.\n\n---\n\n## License\n\n[MIT](LICENSE)\n\n---\n\n## Links\n\n- 🛠️ [php-src/true-async-api](https://github.com/true-async/php-src)\n- 🔌 [php-async](https://github.com/true-async/php-async)\n- 📄 [php-true-async-rfc](https://github.com/true-async/php-true-async-rfc)\n\n---\n\n\u003e _PHP TRUE ASYNC — modern async PHP, today!_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrue-async%2Fphp-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrue-async%2Fphp-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrue-async%2Fphp-async/lists"}