问题
I am using codeigniter framework. And i am searching for a best practice to multi-thread using MVC framework of codeigniter.
I am thinking of creating a class in which all the functions will be written to run in a different independent thread.
Please suggest me techniques to multi thread using codeigniter's MVC framework.
回答1:
You can't do multi-threading directly with PHP. Every page load is handled by a single thread.
If you want the value of function foo()
to be passed to function bar()
but in a multi-threaded kind of way, you could emulate it with a job queue like Resque or RabbitMQ.
But saying you want multi-threaded PHP is like saying you want a motorbike with four wheels.
回答2:
Here is a php multi thread library to fork a process into a separate thread, which i am using to exactly what you describe in mcodeigniter
https://github.com/motanelu/php-thread
回答3:
php-thread doesn't provide real threading, it just forks the process and results in multiprocessing.
If you want real threading in php, use pthreads. But be aware of the pitfalls!
Important note to read
- https://gist.github.com/krakjoe/6437782
来源:https://stackoverflow.com/questions/29241050/how-to-multi-thread-using-codeigniter-fameworkphp