How to notify user when async task ends in PHP/ Windows
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When a PHP application starts a long-running task, the hard part is usually not the background work itself. The harder problem is telling the user that the work finished, especially on Windows where many examples assume Unix process tools that do not exist.
Separate the Job from the Notification
In a web application, the browser and the PHP worker do not share a live connection by default. That means the reliable pattern is:
- Start a background job and return a job ID.
- Store status somewhere shared, such as a file, database row, or Redis key.
- Let the browser poll or subscribe for status updates.
The Windows operating system matters when starting the worker process, but the notification usually belongs in the browser, not the server console.
Starting a Background Task on Windows
One simple Windows-friendly approach is to launch a second PHP process with start /B. The main request returns immediately while the worker updates a status file.
The worker script does the actual work and updates shared status.
This is not a full job queue, but it demonstrates the essential pattern clearly.
Exposing Job Status to the Browser
The client needs a lightweight endpoint that reads the shared status store.
The browser can poll this endpoint every few seconds.
Polling is often enough for admin tools, report generation, imports, and one-off exports. It is easy to deploy and works well with ordinary PHP hosting.
When to Use Something Stronger
If jobs are frequent or critical, add infrastructure instead of stretching plain PHP scripts too far. A database-backed queue, Redis queue, or Windows service gives better durability and monitoring. If the user needs real-time updates without polling, use WebSockets or Server-Sent Events through an application stack that supports persistent connections.
The key design decision is to treat notification as a state change the client can observe. Once that is in place, the transport can evolve without changing the core workflow.
Common Pitfalls
- Trying to push a message directly from a finished PHP worker into an already completed HTTP response. The browser cannot receive data from a closed request.
- Using Unix-specific background process commands on Windows.
nohupand shell syntax from Linux examples do not translate cleanly. - Keeping status only in memory. A second request cannot read in-memory state from a previous PHP process.
- Polling too aggressively. A one or two second interval is usually enough and avoids unnecessary server load.
- Skipping failure states. Store
failedand an error message, not onlycompleted, or users will not know why the job disappeared.
Summary
- On Windows, PHP can start a background worker with
start /B. - The robust pattern is job ID plus shared status storage plus client polling.
- Browser notification usually belongs in JavaScript, not the PHP worker itself.
- Status endpoints should return queued, running, completed, and failed states.
- If throughput grows, move from ad hoc scripts to a proper queue or real-time transport.
Related reading
- How to obtain a Thread id in Python?
- How to obtain a Thread id in Python?
- How to obtain JNI interface pointer JNIEnv for asynchronous calls
- How to parallelize a training loop ever samples of a batch when CPU is only available in pytorch?
- How to parallelize stdpartition using TBB
- How to parallelize stochastic gradient descent?
- How to pass arguments to a thread?
- How to pass async variable in template action function?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.