Implementation of Threading in PHP
Can anybody please describe about how to implement threading in PHP programming? What I know for s开发者_开发问答ure is that PHP does not support thread concept. However, please correct me if I'm wrong.
If possible, please try to provide some details with one example about PHP threading.
If possible, can anybody please also provide some details on thread-safety of PHP modules? I know this is somewhat hot topic in the market, but without any nice & understandable details for dummies like me.
Any help is greatly appreciated.
Threading is on the development roadmap for PHP, but it's not possible with current versions of PHP. There's no thread spawning functions, no thread handling functions, no thread synchronization functions, etc... As well, most PHP plugins/modules/libraries would need to be rewritten to handle threading safely, as they were not built with anything but single-threaded execution in mind.
As one contrived example... you're doing a data import script in PHP and fire off multiple threads to parse chunks of data and insert them into a database. What will mysql_insert_id()
return? Since nothing's thread aware in PHP, you might very well get the ID from an insert operation performed in an entirely different thread.
You can certainly use PHP to fork()
, popen()
, proc_open()
, etc... to up fire multiple copies of a script, and run those concurrently, but that's just multi-processing. Each will have its own environment, memory, file handles, etc...
5 years later ...
Hello
Here is an example on how to implement Thread in PHP :
class Test extends Thread {
public function run() {
print "I'm running in new Thread, i will finish my job in 20 seconds";
sleep(20);
}
}
test = new Test();
test->start();
print "I'm the main thread and i don't have to wait 20 seconds";
Here the link for the official documentation PHP_PTHREADS
Hope that helps :)
精彩评论