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?
You could simulate threading. PHP can run background processes via popen (or proc_open). Those processes can be communicated with via stdin and stdout. Of course those processes can themselves be a php program. That is probably as close as you'll get.
Multithreading means performing multiple tasks or processes simultaneously, we can achieve this in php by using following code,although there is no direct way to achieve multithreading in php but we can achieve almost same results by following way.
}
Test_1.php
This will execute test_1.php two times simultaneously and both process will run in the background simultaneously ,so in this way you can achieve multithreading in php.
This guy done really good work Multithreading in php
You can have option of:
While you can't thread, you do have some degree of process control in php. The two function sets that are useful here are:
Process control functions http://www.php.net/manual/en/ref.pcntl.php
POSIX functions http://www.php.net/manual/en/ref.posix.php
You could fork your process with pcntl_fork - returning the PID of the child. Then you can use posix_kill to despose of that PID.
That said, if you kill a parent process a signal should be sent to the child process telling it to die. If php itself isn't recognising this you could register a function to manage it and do a clean exit using pcntl_signal.
For HTTP multithreading by using GuzzleHttp (as pthreads may not work in web applications), please see below blog - https://blog.madewithlove.be/post/concurrent-http-requests/
You can use exec() to run a command line script (such as command line php), and if you pipe the output to a file then your script won't wait for the command to finish.
I can't quite remember the php CLI syntax, but you'd want something like:
I think quite a few shared hosting servers have exec() disabled by default for security reasons, but might be worth a try.