Giter Site home page Giter Site logo

cthread's Introduction

CThread

https://github.com/amal/CThread

CThread is a simple and powerful threads emulation component for PHP.

Main features and possibilites:

  • Uses forks to operate asynchronously;
  • Synchronous compatibility mode if there are no required extensions;
  • Reuse of the child processes;
  • Full exchange of data between processes. Sending arguments, receiving results;
  • Uses libevent with socket pairs for inter-process communication;
  • Supports five variants of data transfering (for processing arguments, results and event data) between processes and automatically selects the best of them: igbinary serialized data through sockets, php serialized data through sockets, System V Memory queue (sysvmsg), System V shared memory (sysvshm), Shared memory (shmop).
  • Transfer of events between the "thread" and the parent process;
  • Working with a thread pool with preservation of multiple use, passing arguments and receiving results;
  • Errors handling;
  • Timeouts for work, child process waiting, initialization;
  • Maximum performance;

CThread is written for Anizoptera CMF by Amal Samally (amal.samally at gmail.com)

Requirements

Examples

Simply run processing asynchronously

class ExampleThread extends CThread
{
	protected function process()
	{
		// Some work here
	}
}

$thread = new ExampleThread();
$thread->run();

Send argument and receive result of processing

class ExampleThread extends CThread
{
	protected function process()
	{
		return $this->getParam(0);
	}
}

$thread = new ExampleThread();
$result = $thread->run(123)->wait()->getResult();

Triggering events from thread

class ExampleThread extends CThread
{
	const EV_PROCESS = 'process';

	protected function process()
	{
		$events = $this->getParam(0);
		for ($i = 0; $i < $events; $i++) {
			$event_data = $i;
			$this->trigger(self::EV_PROCESS, $event_data);
		}
	}
}

// Additional argument.
$additionalArgument = 123;

$thread->bind(ExampleThread::EV_PROCESS, function($event_name, $event_data, $additional_arg)  {
	// Event handling
}, $additionalArgument);

$events = 10; // number of events to trigger

$thread = new ExampleThread();
$thread->run($events)->wait();

Use pool with 8 threads

$threads = 8  // Number of threads
$pool = new CThreadPool('ExampleThread', $threads);

$num = 25;    // Number of tasks
$left = $num; // Remaining number of tasks

do {
	// If the pool  has waiting threads
	// And we still have tasks to perform
	while ($pool->hasWaiting() && $left > 0) {
		// You get thread id after start
		$threadId = $pool->run();
		$left--;
	}
	if ($results = $pool->wait($failed)) {
		foreach ($results as $threadId => $result) {
			// Successfully completed task
			// Result can be identified
			// with thread id ($threadId)
			$num--;
		}
	}
	if ($failed) {
		// Error handling.
		// The work is completed unsuccessfully
		// if the child process has died at run time or
		// work timeout exceeded.
		foreach ($failed as $threadId) {
			$left++;
		}
	}
} while ($num > 0);

// Terminating all child processes. Cleanup of resources used by the pool.
$pool->cleanup();

Other examples can be seen in the example file (example.php) and unit test (tests/Test_Thread.php).

You can also run the performance tests, choose the number of threads and pick the best settings for your configuration by using a test.php.

Links

CThread — многопоточность для PHP с блэкджеком

cthread's People

Contributors

amal avatar bladeofsteel avatar erkan-yilmaz avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

erkan-yilmaz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.