Skip to content

Parallel Requests

Arun Prakash edited this page May 17, 2024 · 2 revisions

Parallel Requests

WordpressClient offers a built in way of executing requests in parallel by introducing a class dedicated specifically to this purpose, ParallelWordpress. Currently, only list endpoints are supported.

ParallelWordpress class build requests, then execute all of them parallel and return the results in a sorted order based on their page number.

To fetch first 30 pages in parallel:

 final responses = await client.parallel.list(
    interface: client.posts,
    requestBuilder: () {
        return List.generate(
            30,
            (index) => ParallelRequest(
                page: index + 1,
                request: ListPostRequest(
                    perPage: 15,
                    page: index + 1,
                ),
            ),
        );
    },
);

The responses class will be of type Iterable<ParallelResult<Post>> where each ParallelResult<Post> object in the iterable will contain the page number and the response List<Post> for that particular page.

Performence

Just something i came up with quickly! This depends on your website itself and can vary between each user.

Starting Parallel...
Parallel Time Taken: 3667 ms
Starting Sequential...
Sequential Time Taken: 7437 ms

Difference: 3770 ms (50.69%)

⚡ Don't miss out! wordpress_client also offers an extension method to convert the above responses to a single Iterable<Post>, ordered using their page number. To do this, simply call merge() on the above responses.