Vadimbr Posted January 13, 2012 Share Posted January 13, 2012 Hi, I'm trying to implement multithreading using Threadi https://github.com/danielpoe/Threadi Here is a sample code to what I'm trying to do: require_once(dirname(__FILE__).'/../Loader.php'); class Worker { public function fetchAWebsite($url) { $content = file_get_contents($url); return "fetched website $url - lenght ".strlen($content).PHP_EOL; } } $worker = new Worker(); $startTime = microtime(TRUE); $thread1 = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $thread1->start('http://www.google.de'); $thread2 = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $thread2->start('http://www.aoemedia.de'); $joinPoint = new Threadi_JoinPoint($thread1, $thread2); $joinPoint->waitTillReady(); echo $thread1->getResult(); echo $thread2->getResult(); echo "Time needed in seconds: ". ( microtime(TRUE)-$startTime).PHP_EOL; The problem that I'm having is that I need to call each thread separately by name. Creating 10 or more threads makes a lot redundant code. How can I assign Threadi_ThreadFactory::getReturnableThread to an indexed variable(array) ? I tried something in this lines but with no success: $hArr = array();//handle array for ($i=0;$i<THREADS;$i++) { $thread[$i] = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $thread[$i]->start($site[$i]); array_push($hArr,$thread[$i]-); } $joinPoint = new Threadi_JoinPoint($hArr); $joinPoint->waitTillReady(); What I'm doing wrong? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/254941-assign-a-method-to-an-array/ Share on other sites More sharing options...
Zane Posted January 13, 2012 Share Posted January 13, 2012 It should be as easy as this $threads[] = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $threads[0]->start('http://www.google.de'); $threads[] = Threadi_ThreadFactory::getReturnableThread(array($worker,'fetchAWebsite')); $threads[1]->start('http://www.aoemedia.de'); Quote Link to comment https://forums.phpfreaks.com/topic/254941-assign-a-method-to-an-array/#findComment-1307199 Share on other sites More sharing options...
Vadimbr Posted January 13, 2012 Author Share Posted January 13, 2012 It worked. Thanks. Stupid brackets. But now the $joinPoint = new Threadi_JoinPoint($thread); gives me an error, it's expecting list of variables separated by commas. Quote Link to comment https://forums.phpfreaks.com/topic/254941-assign-a-method-to-an-array/#findComment-1307209 Share on other sites More sharing options...
Adam Posted January 13, 2012 Share Posted January 13, 2012 Just pass $thread[0] and $thread[1]: $joinPoint = new Threadi_JoinPoint($thread[0], $thread[1]); Quote Link to comment https://forums.phpfreaks.com/topic/254941-assign-a-method-to-an-array/#findComment-1307247 Share on other sites More sharing options...
Vadimbr Posted January 13, 2012 Author Share Posted January 13, 2012 What happens if I want to open 10000 threads? Is there some variable assignment to do it? Quote Link to comment https://forums.phpfreaks.com/topic/254941-assign-a-method-to-an-array/#findComment-1307253 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.