0

Can anybody please describe about how to implement threading in PHP programming? What I know for sure 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.

2
  • Can you please specify exactly for what purpose you want to use threading. Do you want to run two or more PHP scripts simultaneously to perform one major operation?? Commented Aug 27, 2010 at 5:22
  • @Maulik - I don't have any such example in mind. I'm just trying to clear up the thread concepts with real-world examples available. Please provide such examples with some programming logic also. Commented Aug 27, 2010 at 5:58

2 Answers 2

2

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...

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for such an informative answer. But can you also please highlight about the thread-safety modules in PHP, as there's some mentioning about them, although not totally clear about those for dummies. Once again, thanks.
Mostly they're there so PHP can run under IIS, which is a multithreaded server. But a PHP script under IIS still can't start its own threads - there's simply nothing in the language that allows it.
0

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.