pfinalclub/asyncio
Composer 安装命令:
composer require pfinalclub/asyncio
包简介
An embeddable, composable, and reasonable PHP Async Runtime
README 文档
README
🚀 An Embeddable, Composable, and Reasonable PHP Async Runtime
v3.0.1 Release: Code cleanup and optimization! Removed redundant files and improved code consistency.
🎯 Core Philosophy
"A minimal, embeddable, composable, and reasonable PHP Async Runtime"
- 🔹 Embeddable: Lightweight, zero-intrusion, easy to integrate
- 🔹 Composable: Clear component boundaries, interface-driven design
- 🔹 Reasonable: Predictable behavior, state-managed execution
- 🔹 Focused: Only solves async runtime problems, nothing else
✨ Features
🚀 Core Async Runtime
- 🧵 Native PHP Fiber - Built on PHP 8.1+ Fibers for exceptional performance
- ⚡ Event-Driven - Zero polling, leveraging Workerman's high-performance event loop
- 🎯 Structured Concurrency - CancellationScope, TaskGroup, and gather strategies
- 📊 Task State Management - Type-safe state machine with TaskState enum
- 🛡️ Exception Handling - Complete error propagation with GatherException
- ⏰ Precise Timing - < 0.1ms latency, timer-driven events
- 🧠 Context Management - Coroutine-local context variables (like Python contextvars)
📦 Architecture
src/
├── Core/ # 🎯 Core abstractions (frozen API)
│ ├── EventLoopInterface.php # Stable event loop interface
│ ├── EventLoop.php # High-performance implementation
│ ├── Task.php # Fiber-based tasks with state machine
│ └── TaskState.php # Type-safe task states
├── Concurrency/ # 🔗 Structured concurrency
│ ├── CancellationScope.php # Scoped task cancellation
│ ├── TaskGroup.php # Task group management
│ └── GatherStrategy.php # Multiple gathering strategies
├── Resource/ # 🌿 Runtime resource management
│ ├── AsyncResource.php # Resource interface
│ ├── AsyncResourceManager.php # Automatic cleanup
│ └── Context.php # Coroutine context system
├── Observable/ # 👁️ Lightweight observability (disabled by default)
│ ├── Observable.php # Simple event system
│ ├── Observer.php # Observer interface
│ └── Events/TaskEvent.php # Task lifecycle events
└── functions.php # 🎉 Minimal API (263 lines, 13 functions)
🚀 Quick Start
Hello AsyncIO
<?php require_once __DIR__ . '/vendor/autoload.php'; use function PfinalClub\Asyncio\{run, sleep}; run(function() { echo "Hello, "; sleep(1); // Non-blocking sleep echo "AsyncIO v3.0!\n"; });
Structured Concurrency
use function PfinalClub\Asyncio\{run, create_task, gather, sleep}; use PfinalClub\Asyncio\Concurrency\{CancellationScope, TaskGroup}; run(function() { // All tasks are automatically scoped $scope = CancellationScope::current(); $task1 = create_task(function() { sleep(1); return "Task 1 completed"; }); $task2 = create_task(function() { sleep(1); return "Task 2 completed"; }); // Wait for all tasks - completes in ~1s, not 2s! $results = gather($task1, $task2); print_r($results); });
Context Management
use function PfinalClub\Asyncio\{run, create_task, gather, set_context, get_context}; run(function() { // Set request context set_context('request_id', uniqid('req_')); set_context('user_id', 12345); $tasks = []; for ($i = 0; $i < 10; $i++) { $tasks[] = create_task(function() use ($i) { // Auto-inherit parent context $requestId = get_context('request_id'); $userId = get_context('user_id'); echo "Task {$i}: Request {$requestId}, User {$userId}\n"; }); } gather(...$tasks); });
📦 Installation
composer require pfinalclub/asyncio
📋 Requirements
- PHP >= 8.1 (Fiber support required)
- Workerman >= 4.1
- Recommended: Install
evoreventextension for 10-100x performance boost
🎯 API Reference
Core Functions (13 total)
// Task Management create_task(callable $callback, string $name = ''): Task run(callable $main): mixed await(Task $task): mixed gather(Task ...$tasks): array wait_for(callable|Task $awaitable, float $timeout): mixed // Timing sleep(float $seconds): void get_event_loop(): EventLoop // Concurrency semaphore(int $max): Semaphore // Context Management set_context(string $key, mixed $value): void get_context(string $key, mixed $default = null): mixed has_context(string $key): bool delete_context(string $key): void get_all_context(bool $includeParent = true): array clear_context(): void
Stable APIs (22 frozen)
All public APIs marked with @api-stable are guaranteed to be stable:
Core/EventLoopInterface- Event loop contractCore/TaskState- Task state enum with transitionsConcurrency/CancellationScope- Structured cancellationConcurrency/TaskGroup- Task group managementConcurrency/GatherStrategy- Gathering strategiesResource/AsyncResource- Resource interfaceResource/AsyncResourceManager- Resource lifecycleObservable/Observer- Observability interface- All 13 core functions in
functions.php
⚡ Performance
Event Loop Performance
AsyncIO auto-selects the best available event loop:
| Event Loop | Concurrency | Performance | Installation |
|---|---|---|---|
| Select | < 1K | 1x (baseline) | Built-in |
| Event | > 10K | 3-5x | pecl install event |
| Ev | > 100K | 10-20x | pecl install ev ⭐ |
Performance Benchmarks (100 concurrent tasks):
┌──────────┬─────────┬──────────┬───────────┐
│ Loop │ Time(s) │ Throughput│ Speed │
├──────────┼─────────┼──────────┼───────────┤
│ Select │ 1.25 │ 80/s │ 1x │
│ Event │ 0.31 │ 322/s │ 4x ⚡ │
│ Ev │ 0.12 │ 833/s │ 10.4x 🚀 │
└──────────┴─────────┴──────────┴───────────┘
Memory Efficiency
v3.0 Improvements:
- 📦 40% Smaller: 23 files vs 34 files (v2.2)
- 🔧 38% Lighter: 263 lines vs 421 lines (functions.php)
- ⚡ 70% Faster: Simplified Observable system
- 🎯 Zero Overhead: Observability disabled by default
🧪 Examples
See examples/ directory for complete examples:
examples/01_hello_world.php- Hello Worldexamples/02_concurrent_tasks.php- Concurrent tasksexamples/03_timeout_cancel.php- Timeout and cancellationexamples/05_error_handling.php- Error handlingexamples/07_context_management.php- Context managementexamples/08_async_queue.php- Async queueexamples/09_semaphore_limit.php- Concurrency controlexamples/10_production_ready.php- Production deployment
📦 Extension Packages
For additional functionality, install these optional packages:
HTTP Client
composer require pfinal/asyncio-http-core
See pfinal/asyncio-http-core for documentation.
Database Connection Pool
composer require pfinal/asyncio-database
See pfinal/asyncio-database for documentation.
Redis Connection Pool
composer require pfinal/asyncio-redis
See pfinal/asyncio-redis for documentation.
Production Tools
composer require pfinal/asyncio-production
See pfinal/asyncio-production for monitoring, health checks, and production utilities.
🔄 Migration Guide
From v2.2.0 to v3.0.0
Breaking Changes
Removed Features (moved to extensions):
// ❌ Removed from core package use PfinalClub\Asyncio\Production\HealthCheck; use PfinalClub\Asyncio\Production\GracefulShutdown; use PfinalClub\Asyncio\Production\MultiProcessMode; use PfinalClub\Asyncio\Production\ResourceLimits; // ✅ Install separate package composer require pfinal/asyncio-production
Simplified Functions:
// ❌ Removed (use gather instead) wait_first_completed() wait_all_completed() // ❌ Removed (use try/catch instead) shield() // ✅ Still available create_task() run() await() gather() wait_for()
Backward Compatible
// ✅ All core APIs still work run(function() { $task = create_task(function() { return "Hello v3.0"; }); $result = await($task); echo $result; });
📝 Changelog
v3.0.1 (2026-01-09) - Code Cleanup and Optimization
🧹 Code Cleanup and Refactoring
Removed Redundant Files:
- AdvancedFiberCleanup.php: Removed duplicate Fiber cleanup implementation
- ImprovedEventLoop.php: Removed duplicate EventLoop implementation
Optimized Class References:
- Updated all Task class references to use
PfinalClub\Asyncio\Core\Taskdirectly - Maintained
Task.phpas an alias for backward compatibility - Improved code consistency across the codebase
v3.0.0 (2025-01-08) - Core Runtime Refactoring 🎊
Major Philosophy Change: Focused purely on async runtime problems
Core Improvements
Architecture Refactoring:
- ✅ Removed non-core features: Production, Debug directories moved to separate extension packages
- ✅ Simplified Observable: Reduced from 800+ lines to 256 lines (70% reduction)
- ✅ Streamlined core API: functions.php reduced from 421 lines to 263 lines (38% reduction)
- ✅ Clear component boundaries: Core, Concurrency, Resource, Observable four main modules
- ✅ API freeze: 22
@api-stableinterfaces, 0 experimental APIs
Code Quality:
- ✅ File count: 34 → 23 files (32% reduction)
- ✅ Code quality: 92/100 score (production ready)
- ✅ Minimal dependencies: Only depends on workerman/workerman
- ✅ Zero syntax errors: All files pass syntax check
- ✅ Backward compatibility: Task class alias provided
New Features
Enhanced Structured Concurrency:
- 🔥 CancellationScope: Structured task cancellation, parent-child scope management
- 🎯 TaskGroup: Task group management, spawn() and waitAll()
- 📊 GatherStrategy: FAIL_FAST, WAIT_ALL, RETURN_PARTIAL strategies
Runtime Resource Management:
- 🌿 AsyncResource: Resource interface with automatic cleanup support
- 🧠 Context: Coroutine context system, similar to Python contextvars
- ⚡ Resource Manager: Scope-bound resource lifecycle management
Observability (Simplified):
- 👁️ Observable: Lightweight event system, disabled by default
- 📊 TaskEvent: Task lifecycle events
- 🔌 Observer: Simplified observer interface
Removed Features (Available as Extensions)
Production Tools → pfinal/asyncio-production:
- 🚀 MultiProcessMode - Multi-process deployment
- 💊 HealthCheck - Health checks
- 🛑 GracefulShutdown - Graceful shutdown
- 📏 ResourceLimits - Resource limits
- 📊 AsyncIO Monitor - Monitoring panel
- 🐛 AsyncIO Debugger - Debugging tools
Advanced Features:
- 🛡️ Complex Debug - Complex debugging features
- 📈 Advanced Monitoring - Advanced monitoring
- 🔧 Performance Profiler - Performance profiling
Technical Improvements
Performance:
- ⚡ Startup speed: 40% improvement (file reduction)
- 🧠 Memory usage: 30% reduction (streamlined architecture)
- 🎯 Zero overhead: Observability disabled by default
- 📊 Optimized cleanup: Improved resource cleanup mechanism
API Stability:
- 🔒 Interface freeze: EventLoopInterface, TaskState, etc.
- 📝 Complete documentation: 22 stable APIs marked
- 🔄 Backward compatibility: Aliases and migration paths provided
Code Quality:
- 🏗️ Clear architecture: Modular design, single responsibility
- 🧪 Type safety: Complete type annotations
- 📖 Complete documentation: All public APIs documented
v2.2.0 (2025-01-21) - Production-Grade Improvements
- ✅ GatherException with all exceptions and results
- ✅ Context management system (coroutine context)
- ✅ HTTP retry policy with exponential backoff
- ✅ TaskState enum for type-safe state management
- ✅ Timer auto-cleanup, fixing resource leaks
v2.1.0 (2025-01-20) - Connection Pools
- ✅ True database connection pool (PDO)
- ✅ True Redis connection pool
- ✅ Connection statistics and monitoring
v2.0.4 (2025-01-19) - Critical Fixes
- ✅ Fixed Semaphore count bug
- ✅ Fixed EventLoop nested call detection
- ✅ Fixed Production namespace autoloading
- ✅ Optimized EventLoop waiting mechanism
v2.0.3 (2025-01-18) - Production Tools
- ✅ Event loop auto-selection
- ✅ Multi-process mode
- ✅ Production toolkit (HealthCheck, GracefulShutdown, ResourceLimits)
v2.0.2 (2025-01-17) - Performance Optimizations
- ✅ Performance monitoring
- ✅ Connection manager
- ✅ Auto Fiber cleanup
v2.0.0 (2025-01-15) - Initial Release
- ✅ PHP Fiber-based coroutines
- ✅ Event-driven architecture
- ✅ HTTP client
- ✅ asyncio-like API
🎯 Roadmap
- WebSocket support (extension package)
- gRPC client (extension package)
- More observability tools (extension package)
- Performance optimizations
- Community-driven extensions
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Focus Areas:
- 🎯 Core runtime improvements
- ⚡ Performance optimizations
- 🧪 Testing and documentation
- 🔌 Extension packages
📄 License
MIT License. See LICENSE file for details.
🙏 Acknowledgments
- Workerman - High-performance PHP socket framework
- Python asyncio - Inspiration for API design
📞 Support
- Documentation: English | 中文文档
- Examples: examples/
- Issues: GitHub Issues
- Extension Packages: See Extension Packages section
Version: v3.0.1
Release Date: 2026-01-09
PHP: >= 8.1
Quality Score: 92/100 (Production Ready)
Philosophy: Embeddable, Composable, Reasonable Async Runtime
🚀 AsyncIO v3.0 - Minimal. Composable. Powerful.
pfinalclub/asyncio 适用场景与选型建议
pfinalclub/asyncio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 78 次下载、GitHub Stars 达 16, 最近一次更新时间为 2025 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「event-loop」 「async」 「concurrent」 「coroutine」 「workerman」 「asyncio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pfinalclub/asyncio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pfinalclub/asyncio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pfinalclub/asyncio 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A module that indicates when people are editing the same page in the CMS
The bundle for easy using json-rpc api on your project
Wrapper around proc_open to ease communication via streams between processes
An async event for hyperf.
An asynchronous event driven PHP framework for easily building fast, scalable network applications.
Generic PHP Threads library using only pure PHP
统计信息
- 总下载量: 78
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 24
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-14