Giter Site home page Giter Site logo

php-async-eventdispatch's Introduction

php-async-eventdispatch

I want my web apps to be really fast, and not have the overhead of system events, but unfortunately I can't fork processes under apache.

This implementation, handles events in a new process by using shell_exec/system commands (environment variables are passed to process from cli). All listener methods are executed in the new process and can emit thier own events (up to max recursive depth) and othewise take as long as they need.

pcntl_fork is used/attempted in the handler process (which should have become available now that it is outside of apache) to avoid problems with multiple listeners, ie: if the first listener sleeps for some time then other listeners might have to wait unless you explicitly enable forking: 'fork' => true, when instantiating the dispatcher, forking will not be used.

Usage - Configuration;

require __DIR__.'/vendor/autoload.php';

$dispatcher=new asyncevent\AsyncEventDispatcher(array(
	'command'=>'php '.__FILE__, 
	'getEnvironment'=>function(){
		//get some environment variables to pass to shell_exec on cli
		//put whatever you need here this is just an example
		return array(
			'session'=>'0000000000',
			'access_token'=>'0000000000'
			//things like domain name, ip address, web browser might be useful
		);
	}
	//, 'log'=>function($message){} or String path
));

if($dispatcher->shouldHandleEvent()){


	$dispatcher->handleEvent(
		array(
			//'fork' => true,
			'setEnvironment' => function($env){
		
				// these are the variables you passed to the emitter, they came back from the command line
				$system->setSession($env->session);
				$system->setUserFromAccessToken($env->access_token)
				
			},
			'getEventListeners'=>function($event)use(&$dispatcher){
		
				// resolve event listeners. 
				// whatever you return here will just become available to you 
				// in 'handleEvent' below, so it could be objects or strings, ids...
				return array(
					instantiateSomeClass(...)
				);

			},
			'handleEvent'=>function($listener, $event, $eventArgs){
				
				$listener->someEventMethod($event, $eventArgs);
				
			}			
		)
	);
	return; //this process should be finished after executing events.
}


// render web app ...  

// $dispatcher->emit($event, $eventArgs);

// schedule an event to be executed later
// $dispatcher->schedule($event, $eventArgs, $seconds);

php-async-eventdispatch's People

Contributors

nickolanack avatar

Stargazers

Sara Tasche avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

guoyu07

php-async-eventdispatch's Issues

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.