Skip to content

Commit f8f65d4

Browse files
When getting results via a callback function, dont store the results, so that we use as little memory as possible
1 parent 28072af commit f8f65d4

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/Crusse/JobServer/Server.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ function addWorkerInclude( $phpFilePath ) {
5252
$this->workerIncludes[] = $phpFilePath;
5353
}
5454

55-
function getResults( $jobCallback = null ) {
55+
function getOrderedResults() {
5656

57-
if ( $jobCallback && !is_callable( $jobCallback ) )
58-
throw new \InvalidArgumentException( '$jobCallback is not callable' );
59-
60-
$this->jobCallback = $jobCallback;
57+
$this->jobCallback = null;
6158

6259
$loop = new EventLoop( $this->serverSocketAddr );
6360
$loop->listen( $this->workerTimeout );
@@ -75,6 +72,23 @@ function getResults( $jobCallback = null ) {
7572
return $results;
7673
}
7774

75+
function getResults( $jobCallback ) {
76+
77+
if ( !is_callable( $jobCallback ) )
78+
throw new \InvalidArgumentException( '$jobCallback is not callable' );
79+
80+
$this->jobCallback = $jobCallback;
81+
82+
$loop = new EventLoop( $this->serverSocketAddr );
83+
$loop->listen( $this->workerTimeout );
84+
$loop->subscribe( array( $this, '_messageCallback' ) );
85+
86+
if ( !$this->workerProcs )
87+
$this->createWorkerProcs( $this->workerCount );
88+
89+
$loop->run();
90+
}
91+
7892
// FIXME: workerTimeout isn't working yet. A sleep()ing worker does _not_
7993
// cause a timeout. Check why.
8094
function setWorkerTimeout( $timeout ) {
@@ -110,7 +124,10 @@ function _messageCallback( Message $message, EventLoop $loop, $socket ) {
110124
if ( $headers[ 'cmd' ] === 'job-result' ) {
111125

112126
$jobNumber = $headers[ 'job-num' ];
113-
$this->results[ $jobNumber ] = $message->body;
127+
// Only store the results if the client called getOrderedResults()
128+
$this->results[ $jobNumber ] = ( $this->jobCallback )
129+
? true
130+
: $message->body;
114131

115132
if ( $this->jobCallback ) {
116133
call_user_func( $this->jobCallback, $message->body, count( $this->results ),

tests/test.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,31 @@ function generateString($length) {
2222
$server = new Crusse\JobServer\Server( 4 );
2323
$server->addWorkerInclude( __DIR__ .'/functions.php' );
2424
$server->setWorkerTimeout( 2 );
25-
2625
for ( $i = 0; $i < 50; $i++ )
2726
$server->addJob( 'job_test', 'Job '. $i .': '. generateString( 50 ) );
2827

29-
$time = microtime( true );
30-
$res = $server->getResults( function( $result, $jobNumber, $total ) {
28+
echo 'Results with callback:'. PHP_EOL . PHP_EOL;
29+
30+
$server->getResults( function( $result, $jobNumber, $total ) {
3131
echo 'Job '. $jobNumber .'/'. $total .': '. $result . PHP_EOL;
3232
} );
33+
34+
echo PHP_EOL .'Ordered results:'. PHP_EOL . PHP_EOL;
35+
36+
$server = new Crusse\JobServer\Server( 4 );
37+
$server->addWorkerInclude( __DIR__ .'/functions.php' );
38+
$server->setWorkerTimeout( 2 );
39+
for ( $i = 0; $i < 50; $i++ )
40+
$server->addJob( 'job_test', 'Job '. $i .': '. generateString( 50 ) );
41+
42+
$time = microtime( true );
43+
44+
$res = $server->getOrderedResults();
45+
3346
$elapsed = ( microtime( true ) - $time ) * 1000;
3447
$elapsedTotal = ( microtime( true ) - $timeTotal ) * 1000;
3548

36-
//var_dump( $res );
49+
echo implode( PHP_EOL, $res ) . PHP_EOL . PHP_EOL;
3750
echo 'Finished in '. $elapsed .' ms'. PHP_EOL;
3851
echo 'Total '. $elapsedTotal .' ms'. PHP_EOL;
3952

0 commit comments

Comments
 (0)