PHPackages                             irap/async-query - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. irap/async-query

ActiveLibrary[Database &amp; ORM](/categories/database)

irap/async-query
================

Package to make asynchronous MySql queries a breeze.

2.1.0(9y ago)22.2k↓50%MITPHPPHP &gt;=5.5.0

Since Nov 8Pushed 9y ago1 watchersCompare

[ Source](https://github.com/iRAP-software/package-async-query)[ Packagist](https://packagist.org/packages/irap/async-query)[ Docs](https://github.com/iRAP-software)[ RSS](/packages/irap-async-query/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (13)Used By (0)

Async Query
===========

[](#async-query)

This package was created to make it simple to create asynchronous MySql logic. Using asynchronous database queries can dramatically improve your applications performance by removing all of the wait/idle time spent waiting for the connections to be made and for queries to execute on the database.

Requirements
------------

[](#requirements)

- [native mysql driver (msyqlnd)](https://secure.php.net/manual/en/book.mysqlnd.php).
    - Install on ubuntu with `sudo apt-get install php5-mysqlnd -y`
- This package was built/tested using PHP 5.5. It may or may not work on earlier versions, but please be aware that [any PHP version lower than 5.5 is deprecated](https://secure.php.net/supported-versions.php).

Connection Pools
----------------

[](#connection-pools)

Asynchronous queries rely on having their own MySql connection. To prevent developers reaching their databases connection limit, our `AsyncQuery` object relies on being passed a `MysqliConnectionPool` object which takes a connection limit as one of its required parameters. This object will provide the asynchronous queries with a connection when one becomes available.

AsyncQuery
----------

[](#asyncquery)

The AsyncQuery object takes a callback. It is important to remember that this callback is provided the result of executing the SQL statement it was told to execute. This result can be a `mysqli_result` object or `FALSE` if the query failed. It is in this callback where you should put any code that works with the result or should execute when the query is finished.

### Example Usage:

[](#example-usage)

```
# Create the connection pool
$connectionPool = new \iRAP\AsyncQuery\MysqliConnectionPool(
    5, # max connections
    DB_HOST,
    DB_USER,
    DB_PASSWORD,
    DB_NAME
);

$sql =  "SHOW TABLES";

$queryCallback = function($result) {
    if ($result === FALSE)
    {
        throw new Exception("query failed!");
    }
};

$asyncQuery = new \iRAP\AsyncQuery\AsyncQuery(
    $sql,
    $queryCallback,
    $connectionPool
);

# Execute the query and wait for its result
while ($asyncQuery->run() === FALSE)
{
    usleep(1);
}

```

Queues
------

[](#queues)

There are three queue objects that I recommend using:

- SerialRunnableQueue - run items one after each other (FIFO), waiting for each one to complete before moving onto the next.
- ParallelRunnableQueue - run the items in parallel. These items could be executed/completed *in any order*. This is where performance benefits are realized.
- RunnableStack - Execute items in the same manner as `SerialRunnableQueue` except that instead of being FIFO, items are executed in reverse order with the last item being added being executed first.

Each of these queues take `RunnableInterface` objects which are AsynqQuery objects or other queues. This allows you to create any combination that you need. For example, you may have several "task types" that have to be executed in order, however each of these "task types" might consist of several hundred tasks that can be executed in any order. In such a case, you would want to create a `ParallelRunnableQueue` for each of these task types containing all the parallel tasks and then place those `ParallelRunnableQueue` objects in a single `SerialRunnableQueue` object.

Every queue can take an optional callback in its constructor. This callback is executed whenever the queue is emptied. The main reason for this is that it allows the developer to run logic that can only be run when all of the tasks in a group have finished. However, you could use this logic to run some logic that would result in more queries being added to the queue etc.

Queues are not a fixed size, you can use the `add` method to add to them as and when you like. However, remember that the callback will be invoked each time the queue is depleted.

Queues, like the `AsyncQuery` object, do nothing on their own. The developer needs to execute their `run()` method for them to execute their queries. The `run()` method will return true if the query/queue completed/depleted.

### Example Usage:

[](#example-usage-1)

```
...

$asyncQuery1 = new \iRAP\AsyncQuery\AsyncQuery(
    $sql1,
    $queryCallback1,
    $connectionPool
);

$asyncQuery2 = new \iRAP\AsyncQuery\AsyncQuery(
    $sql2,
    $queryCallback2,
    $connectionPool2 # add($asyncQuery1);
$parallelRunnableQueue->add($asyncQuery2);

# Run until the queue has completed all of the tasks.
while ($parallelRunnableQueue->run() !== TRUE)
{
    usleep(1);
}

```

Automated Tests
---------------

[](#automated-tests)

From 2.0.0 and onwards automated testing has been introduced. Simply go to the `testing` directory, rename the `Settings.php.tmpl` to `Settings.php` and fill in your database details. Then execute the `main.php` script. All code contributions should provide a relevant test case.

It may be a good idea to read through the automated tests to get example usages.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 55.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~41 days

Recently: every ~112 days

Total

12

Last Release

3390d ago

Major Versions

1.1.1 → 2.0.02015-11-08

PHP version history (2 changes)1.0.0PHP &gt;=5.3.0

2.1.0PHP &gt;=5.5.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/52783970b96ca5954e3335aacc48870d8db7ee70dad5adaa0170a37e0b028566?d=identicon)[irap\_admin](/maintainers/irap_admin)

---

Top Contributors

[![programster](https://avatars.githubusercontent.com/u/5709838?v=4)](https://github.com/programster "programster (20 commits)")[![iRAP-Stu](https://avatars.githubusercontent.com/u/115175332?v=4)](https://github.com/iRAP-Stu "iRAP-Stu (16 commits)")

---

Tags

mysql

### Embed Badge

![Health badge](/badges/irap-async-query/health.svg)

```
[![Health](https://phpackages.com/badges/irap-async-query/health.svg)](https://phpackages.com/packages/irap-async-query)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k25.2M34](/packages/kirschbaum-development-eloquent-power-joins)[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58723.9M36](/packages/scienta-doctrine-json-functions)[cytopia/mysqldump-secure

Secure mysqldump script with encryption, compression, logging, blacklisting and Nagios monitoring integration

1474.7k1](/packages/cytopia-mysqldump-secure)[ark/database

Light weight database abstraction

117.8k](/packages/ark-database)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
