I have code structure like this
echo "First Operation";
sleep(3);
echo "Second Operation";
I want to try simulate multi-threading but I could not make, sleep function is meaningless there, but I could not find another option.
I have code structure like this
echo "First Operation";
sleep(3);
echo "Second Operation";
I want to try simulate multi-threading but I could not make, sleep function is meaningless there, but I could not find another option.
I have to agree that PHP is not built for multithread. As such the script you create will always run in one thread. There are some exceptions to that. When you call a shell command there is the possibility to fire-and-forget the command when you dont need the output. It runs parallel but you can not access the result.
PHP is designed to execute one instruction after the other.
To create something like a parallel working script, you need to take advantage of some external systems. All of those tricks are related to your actual solution you wane try to accomplish. Let me give you an example.
You could use a Message Queueing system like Rabbit MQ to work as a separation between one script and the other. Lets say script1 is permanently listening on the queue for work to do. Another script, lets say script2, is (on request) bushing a work into the queue and continuing with other stuff while the script1 is picking that up, doing something and returning the answer. When script2 is done with the other stuff it can read the result from the queu and finish whatever you want to do.
This is actually a very tricky and theoretical concept, but it might give you an idea. But without any external component like the queue, php will not work multithreaded.
I hope that is helping you!