Simplify PHP Asynchronous Programming with TrueAsync and Docker

TrueAsync is a PHP async extension that, together with Docker Compose, PHP‑FPM and Nginx, lets developers write coroutine‑based asynchronous code, provides ready‑made demos, and offers a lightweight, Docker‑first stack for building high‑throughput web services.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Simplify PHP Asynchronous Programming with TrueAsync and Docker

Simplify PHP Asynchronous Programming

TrueAsync is an innovative PHP asynchronous extension that enables developers to use coroutines (spawn, await) for easy async programming. The GitHub repository supplies a complete Docker configuration that tightly integrates TrueAsync PHP with PHP‑FPM and Nginx, allowing rapid setup of an async‑capable web environment.

Key Highlights

TrueAsync converts PHP's blocking I/O into non‑blocking operations, so network requests, file handling, and web scraping can run concurrently, delivering several‑fold performance gains. Its main advantages are:

Docker Integration : No manual extension installation; the image bundles TrueAsync and all dependencies.

Nginx Optimization : Default timeout set to 300 seconds, suitable for long‑running async tasks.

Supervisor Management : Automatically monitors processes to keep services stable.

Built‑in Tests : Includes demos from phpinfo to web‑scraper examples for quick verification.

Compared with a traditional LAMP stack, this configuration is lighter and more modern, making it ideal for micro‑services or API back‑end development.

Quick Start

The repository offers two launch methods; Docker Compose is recommended for its simplicity and extensibility. Assuming Docker and Docker Compose are installed locally:

Clone the repository and prepare the application directory :

git clone https://github.com/true-async/fpm.git
cd fpm
mkdir -p app
echo '<?php phpinfo(); ?>' > app/index.php  # create a simple test page

Start the services : docker-compose up --build After a few minutes the build finishes. Open http://localhost:8080 in a browser to see a welcome page showing TrueAsync information. For a pure Docker workflow you can also run:

docker build -t trueasync-fpm .
docker run -d -p 8080:80 --name trueasync-fpm trueasync-fpm

Visiting http://localhost:8080 confirms the service is up.

Testing

The repository ships several demo files you can try: /phpinfo.php: Shows PHP configuration and the TrueAsync extension (test with curl http://localhost:8080/phpinfo.php). /async-test.php: Basic async task verification. /async-parallel.php: Executes multiple tasks in parallel to observe speed improvements. /async-sleep.php: Demonstrates the delay function. /async-scraper.php: Real‑world web‑scraping example that pulls several pages asynchronously.

These tests not only validate functionality but also inspire production use cases such as high‑throughput crawlers.

First TrueAsync Code Example

Create app/my-async-test.php with the following content:

<?php
use function Async\spawn;
use function Async\await;
use function Async\awaitAll;
use function Async\delay;

echo "Starting async operations...
";

// Example 1: Simple spawn and await
$coroutine = spawn(function() {
    delay(1000); // 1 second
    return "Task completed!";
});
$result = await($coroutine);
echo "$result
";

// Example 2: Parallel execution
$coroutines = [
    spawn(fn() => delay(1000) ?? "Task 1"),
    spawn(fn() => delay(1000) ?? "Task 2"),
    spawn(fn() => delay(1000) ?? "Task 3"),
];
[$results, $exceptions] = awaitAll($coroutines);
print_r($results);

echo "All done!
";
?>

Access http://localhost:8080/my-async-test.php and you will see the tasks completing almost simultaneously, demonstrating the benefit of async execution for e‑commerce recommendation engines or real‑time data aggregation.

Configuration

PHP‑FPM Configuration

Key parameters in www.conf:

pm = dynamic
pm.max_children = 50    # Maximum processes
pm.start_servers = 5    # Starting number
pm.min_spare_servers = 5 # Minimum idle processes
pm.max_spare_servers = 35 # Maximum idle processes

PHP Configuration

Add custom settings to php.ini:

max_execution_time = 300
memory_limit = 256M
upload_max_filesize = 50M
post_max_size = 50M

Logs

View logs with Docker Compose:

# All logs
docker-compose logs -f

# PHP‑FPM only
docker exec -it trueasync-fpm tail -f /var/log/php-fpm/error.log

# Nginx only
docker exec -it trueasync-fpm tail -f /var/log/nginx/error.log
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendDockerPHPasyncTrueAsync
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.