home / skills / hoangnguyen0403 / agent-skills-standard / concurrency
This skill helps you implement PHP concurrency using fibers and async libraries to improve I/O responsiveness.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill concurrencyReview the files below or copy the command above to add this skill to your agents.
---
name: PHP Concurrency
description: Handling concurrency and non-blocking I/O in modern PHP.
metadata:
labels: [php, concurrency, fibers, async]
triggers:
files: ['**/*.php']
keywords: [Fiber, suspend, resume, non-blocking, async]
---
# PHP Concurrency
## **Priority: P2 (MEDIUM)**
## Structure
```text
src/
└── Async/
├── Schedulers/
└── Clients/
```
## Implementation Guidelines
- **Fibers**: Use `Fiber` for low-level cooperative multitasking (8.1+).
- **Yield Control**: Apply `Fiber::suspend()` to yield within Fibers.
- **I/O Bound**: Target I/O tasks only; avoid for CPU intensive work.
- **Frameworks**: Prefer **Amp** or **ReactPHP** for complex events.
- **Self-Contained**: Ensure Fibers manage their own state/exceptions.
- **Incremental**: Refactor single bottlenecks before full async.
## Anti-Patterns
- **Implicit Flows**: **No Deep Suspend**: Keep Fiber logic traceable.
- **Internal Blocking**: **No Blocking I/O**: Don't block inside Fibers.
- **Custom Schedulers**: **No DIY Schedulers**: Use proven async libs.
## References
- [Fiber Implementation Guide](references/implementation.md)
This skill provides practical guidance for handling concurrency and non-blocking I/O in modern PHP. It focuses on using Fibers for cooperative multitasking and recommends established async libraries for complex event-driven work. The goal is safe, incremental adoption that targets I/O-bound bottlenecks without introducing hidden blocking or custom scheduler complexity.
The skill inspects code structure and offers patterns for organizing async components under an Async/ directory with Schedulers and Clients. It advocates using PHP Fibers (8.1+) for low-level cooperative multitasking and shows when to yield control with Fiber::suspend(). For more advanced or production-grade event loops, it recommends integrating Amp or ReactPHP rather than building custom schedulers.
Can I use Fibers for CPU-bound tasks?
No. Fibers provide cooperative multitasking but do not parallelize CPU work. Use process or thread-based approaches for CPU-heavy workloads.
Should I write my own scheduler?
Avoid DIY schedulers. Use proven libraries like Amp or ReactPHP to handle scheduling, backpressure, and event-loop correctness.
How do I prevent blocking inside a Fiber?
Replace blocking calls with non-blocking equivalents offered by your async library, and never call blocking I/O directly from within a Fiber.