Download the PHP package wapmorgan/threadable without Composer

On this page you can find all versions of the php package wapmorgan/threadable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package threadable

Threadable

Easy-to-use threading library providing all basic features to perform work in background mode.

All you need to have installed:

This library can also work in simulation mode, where no actual forking performs. All work is done in one main thread. This mode enables if pnctl extension is not available or when you specify it in Worker constructor.

Latest Stable Version Latest Unstable Version License

  1. Structure
    • What is a Worker?
      • How to create your Worker
    • What is a WorkersPool?
  2. Simple usage
  3. How it works
    • One worker
    • Few workers with WorkersPool
  4. API
    • Worker API
    • WorkersPool API
  5. Predefined workers
    • DownloadWorker
  6. Use cases

Structure

What is a Worker?

Worker - is a basic class for any worker. It is composed of two substances (physically, stored in one class, but providing different functionalities):

  1. A Worker - a separate thread, doing all background work.
  2. A Worker manager - a manipulator for the worker thread.

How to create your Worker

The all you need it to extend wapmorgan\Threadable\Worker class and reimplement onPayload($data) public method.

For example:

What is a WorkersPool?

WorkersPool (wapmorgan\Threadable\WorkersPool) - is a container for Worker's, intended for handling similar tasks. It takes care of all maintenance, payload dispatching and life-cycle of workers. Allows you change the size of the pool dynamically and other useful stuff.

Simple usage

For example, you want to just background downloading work. Let's use wapmorgan\Threadable\BackgroundWork class to background it and show progress for user (or store in DB/...).

Everything you need to do:

  1. Prepare payloads for DownloadWorker
  2. Launch BackgroundWork::doInBackground() or BackgroundWork::doInBackgroundParallel() for one thread or few threads respectively.

Stage 1. Preparing payloads

DownloadWorker needs an array with source and target elements. Prepare it:

Stage 2. Launching in background

One-thread worker

Run it in one thread with doInBackground function. Signature is following:

doInBackground(Worker $worker, array $payloads, callable $payloadHandlingCallback = null, callable $onPayloadFinishCallback = null, $sleepMicroTime = 1000)

So, collect all information to run it:

Example is in bin/example_file_downloading_easy file.

Few-threads worker

To run it in few threads use doInBackgroundParallel. It has almost the same signature as one-thread function:

doInBackgroundParallel(Worker $worker, array $payloads, callable $payloadHandlingCallback = null, callable $onPayloadFinishCallback = null, $sleepMicroTime = 1000, $poolSize = self::BY_CPU_NUMBER)

By adjusting $poolSize you can select number of workers that should be used.

Example is in bin/example_file_downloading_pool_easy file.

How it works

One worker

If you just need to parallel some work and do it in another thread, you can utilize just Worker class without any other dependencies.

To use it correctly you need to understand the life-cycle of worker:

  1. Worker starts in another thread. To do this call start().
  2. Worker accepts new payload and starts working on it. To do this call sendPayload(array $data). Really, worker manager sends payload via local socket. Worker thread starts working on it and returns result of work on finish via the same socket.
  3. Worker manager checks if worker thread has done and read result of work. To do this call checkForFinish().
  4. Worker stops or being killed by stop() or kill() methods respectively.
  5. Worker manager checks if worker thread has finished and marks itself terminated. To do this call checkForTermination().

Background work happens in 2 steps, where worker thread runs onPayload($data) method of class with actual payload.

To summarize, this is an example of downloading file in another thread with real-time displaying of progress:

Settings and structures

Real work

Result:

This code equipped with a lot of comments, but you can simplify this example if you don't need to re-use worker when all your work is done. You can replace this huge loop with a smaller one:

Result:

Few workers with WorkersPool

But what if you need do few jobs simultaneously? You can create few instances of your worker, but it will be pain in the a$$ to manipulate and synchronize them. In this case you can use WorkersPool, which takes care of following this:

  1. Start new workers at the beginning.
  2. Dispatch your payload when you call sendData to any idle worker*.
  3. Create new workers or delete redundant workers when you change poolSize.
  4. Accept result of workers when they done and marks them as idle.
  5. Monitor all worker threads and count idle, running, active (idle or running) workers. Provides interfaces to acquire this information.
  6. Stop all workers when WorkersPool object is being destructed (via unset() or when script execution is going down).
  7. *Can work in dataOverhead-mode. This mode enables sending extra payload to workers even when are already working on any task. If in this mode you sent few payloads to worker, it will not switch to Worker::IDLE state until all passed payloads have been processed.
  8. Provide interface to appoint progress trackers and run them periodically until all threads become in Worker::IDLE state.

Rich feature-set, right?! Let's rewrite our downloader with 2 threads to speed-up downloading.

The Settings and structures block of code remains the same, but for demonstating purposes let's use two big files:

We need to update only code working with threads.

Result:

As you can see, we got few improvements:

  1. Our code became smaller and clearer.
  2. We can run as many workers as we need.
  3. We don't take care of worker termination anymore. Let WorkersPool work for us.

API

Worker API

Information:

Warning about worker re-using! You can't restart a worker that has been terminated (with stop() or kill()), you need to create new worker and start it with start().

WorkersPool API

Predefined workers

DownloadWorker

As you've seen in examples, we created a downloading worker. But there is no need for this, we could use predefined DownloadWorker which does the same.

ExtractWorker

Zip-archives extractor.

Use cases

Examples of programs that can be built with Threadable:


All versions of threadable with dependencies

PHP Build Version
Package Version
No informations.
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package wapmorgan/threadable contains the following files

Loading the files please wait ....