CodeIgniter Forums
How can one use multi threading in PHP applications ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forum-9.html)
+--- Thread: How can one use multi threading in PHP applications ? (/thread-67200.html)



How can one use multi threading in PHP applications ? - makeshaun - 01-26-2017

Is there a realistic way of implementing a multi-threaded model in PHP whether truly, or just simulating it. Some time back it was suggested that you could force the operating system to load another instance of the PHP executable and handle other simultaneous processes.

The problem with this is that when the PHP code finished executing the PHP instance remains in memory because there is no way to kill it from within PHP. So if you are simulating several threads you can imagine whats going to happen. So I am still looking for a way multi-threading can be done or simulated effectively from within PHP. Any ideas?


RE: How can one use multi threading in PHP applications ? - Narf - 01-26-2017

There's always a way, but the question is what do you need it for and is that really the solution.


RE: How can one use multi threading in PHP applications ? - skunkbad - 02-12-2017

If you're on linux, I think you're looking for something like this:

https://github.com/diversen/background-job

Through PHP's exec function, you're able to run a bash command, which could run anything that the www-data user would be able to run. I think the concept is that you would set up some sort of listener to determine when the process has finished.


RE: How can one use multi threading in PHP applications ? - ignitedcms - 02-12-2017

One uses golang or nodejs for such tasks definitely not PHP IMO.


RE: How can one use multi threading in PHP applications ? - skunkbad - 02-12-2017

(02-12-2017, 02:41 PM)iamthwee Wrote: One uses golang or nodejs for such tasks definitely not PHP IMO.

I prefer shell scripts when possible. PHP just runs them. Commands same as CLI. Very powerful. I've noticed PHP scripts that would take 2 mins to do a bunch of MySQL stuff, and CLI did same MySQL in like 3 seconds. Bam!


RE: How can one use multi threading in PHP applications ? - ignitedcms - 02-12-2017

Yes but that is not really multi-threading or utilising parallel processing.

Golang uses the concept of Go routines which are effectively cheap threads. It's definitely worth a read as the syntax is much easier than the threading you would use in either c or c++ and much safer to use.

https://www.youtube.com/watch?v=cN_DpYBzKso

Nodejs is more popular than golang and is the go to for such things nowadays, I think it is single threaded though although you could do something with callbacks. The only trouble is, is it is javascript syntax which I don't particularly enjoy using haha.


RE: How can one use multi threading in PHP applications ? - skunkbad - 02-12-2017

(02-12-2017, 04:41 PM)iamthwee Wrote: Yes but that is not really multi-threading or utilising parallel processing.

Golang uses the concept of Go routines which are effectively cheap threads. It's definitely worth a read as the syntax is much easier than the threading you would use in either c or c++ and much safer to use.

https://www.youtube.com/watch?v=cN_DpYBzKso

Nodejs is more popular than golang and is the go to for such things nowadays, I think it is single threaded though although you could do something with callbacks. The only trouble is, is it is javascript syntax which I don't particularly enjoy using haha.

I watched about 2/3 of the video. I understand that using php's exec by itself would not utilize multi-threading or parallel processing, but it does allow PHP's execution to continue while the command or script run by exec is it's own process.

Regardless of language, I don't have the programming experience to know exactly when I would need true multi-threading. When I'm making Android apps I know I am forced to use a different thread for certain things, but I am not actually doing the hard work there, but instead relying on RxAndroid.

So if the OP really wants to do true multi-threading, then I don't have the answers. If OP wants to do two+ things at once, using the little library I linked to really does work. For my own needs I made some very slight tweaks to it, and it definitely creates new linux processes that can be verified through ps.

PHP Code:
/**
 * Start a background process using the Background_process library.
 */
public function start_background_process()
{
    
$this->load->library('background_process');

    
$pid $this->background_process->runFCPATH 'sh/background_process_test.sh' );

    echo 
anchor('test/monitor_background_process/' $pid'Check if it is running' );
}

// -----------------------------------------------------------------------

/**
 * Monitor the created background process (keep hitting F5)
 */
public function monitor_background_process$pid )
{
    
$this->load->library('background_process');

    echo 
'Running: ' $this->background_process->is_running$pid );
}

// -----------------------------------------------------------------------