2017 © Pedro Peláez
 

library kyew

image

adamnicholson/kyew

  • Sunday, May 28, 2017
  • by adamnicholson
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3,204 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 10 % Grown

The README.md

Kyew

Build Status, (*1)

Kyew is a thin layer on top of your existing queue package allowing you to push tasks to the queue and await the task completing., (*2)

Some examples where this could be useful include:, (*3)

  • Executing multiple tasks concurrently
  • Pushing resource intensive tasks to a more performant server

Requirements

Example

Await a single task

$task = $kyew->async(function () {
    // Do some slow I/O operation
    return 'foo';
});
$response = $task->await(); // (string) "foo"

Execute multiple tasks simultaneously

$google = $kyew->async(function () {
    return file_get_contents('http://google.com');
});

$bbc = $kyew->async(function () {
    return file_get_contents('http://bbc.co.uk');
});

// Both closures have already started executing in the background

$google->await(); // (string) HTML source for Google's homepage 
$bbc->await(); // (string) HTML source for BBC's homepage 

How it Works

When tasks are passed to $task = $kyew->async($task), they are immediately handed off to the underlying queue package, along with an additional instruction to then store the task return value back into a persistance layer (eg. a database)., (*4)

On calling $task->await(), we simply sit in a loop until either that value appears in the persistance layer, or until we reach the timeout threshold., (*5)

Installation

Kyew can be installed with Composer, (*6)

composer require adamnicholson/kyew

Kyew has two dependencies:, (*7)

The below example uses the InMemoryPubSub and SynchronousQueue queue - you should use implementations which suit your environment., (*8)

$pubsub = new Kyew\PubSub\InMemoryPubSub;
$kyew = new Kyew\Kyew(
    $pubsub,
    new Kyew\Queue\SynchronousQueue($pubsub)
);

$task = $this->kyew->async(function () {
    return 'Some return value!!!';
});
echo $task->await(); // string 'Some return value!!!'

API

Kyew::async(callable $task): Task

async accepts a single callable as its only parameter and will return an instance of Task., (*9)

$task = $kyew->async(function () {
    // Do some slow I/O operation
    return 'foo';
});

The callable is immediately handed to the queue library to be executed. The Task instance will listen to the queue process and be notified when the callable has finished executing., (*10)

Task::await(): void

await will block further code execution until the given Task has completed exectuing., (*11)

$response = $task->await();
echo $response; // (string) "foo"

Supported Queue Packages

  • IlluminateQueue: Execute tasks in Laravel's (Illuminate) queue package
  • SynchronousQueue: Execute tasks synchronously in the current process, mostly only used for testing

Supported persistence layers

  • DatabasePubSub: Store task return values in a PDO compatible SQL database
  • RedisPubSub: Store task return values in a redis server
  • InMemoryPubSub: Store task return values in an in-memory PHP array, mostly only used for testing

Contributing

We welcome any contributions to Kyew. They can be made via GitHub issues or pull requests., (*12)

License

Kyew is licensed under the MIT License, (*13)

Author

Adam Nicholson - adamnicholson10@gmail.com, (*14)

The Versions

28/05 2017

dev-master

9999999-dev

  Sources   Download

The Requires

  • jeremeamia/superclosure ~2.1

 

The Development Requires

13/04 2017

0.1.0

0.1.0.0

  Sources   Download

The Requires

  • jeremeamia/superclosure ~2.1

 

The Development Requires