5

I've read everywhere that PHP does not support multithreading but there are workarounds. But why does PHP not support multithreading? For a server-side scripting language, that seems like a glaring omission.

skytreader
  • 11,467
  • 7
  • 43
  • 61
  • 1
    I think you hit it when you said scripting language. It's not really meant so much as a full blown solution to problems, it's more of a glue. If you're doing something that requires threading, you might be better off using something that's more performance oriented than PHP. PHP is ... uh ... quirky, for lack of a less offensive term. – Wug Aug 27 '12 at 19:23
  • Well...Python is often considered "scripting" (though it blurs the line with full-blown PLs). And AFAIK, you can use threads in Python, even in web-dev right? http://docs.python.org/library/threading.html – skytreader Aug 27 '12 at 19:36
  • 1
    You sort of can. Threading in python is somewhat crippled. The same rule applies though, if you're doing something that's seriously efficiency driven, python isn't a good choice. Python is, at least, generally considered to be designed better than PHP. – Wug Aug 27 '12 at 19:39

2 Answers2

10

Read this URL i think it is help full to you.

PHP Multithreading – Faking It

http://w-shadow.com/blog/2007/08/20/php-multithreading-faking-it/

PHP doesn’t really support multi-threading per se but there are ways to do “fake” multithreading. Here’s one I saw in the PHPClasses.org newsletter –

Multi-thread Simulation.

Note that this class is intedend for use on a webserver, as opposed to running PHP scripts from a command line (or similar). Check the end of this post for some alternatives you can try if you’re using PHP as a stand-alone scripting language.

Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
-3

Edit several years later: you can use pthreads for PHP multithreading, but do you really want to?

I'm not sure if pthreads is available on many hosting environments. And frankly I am not in a rush to find out.

One of PHP's biggest strengths for many applications is its isolation: one process, one request. Multi-processing is usually done by queuing a job during a request and executing it somewhere else. For example, Laravel has "queues": https://laravel.com/docs/5.7/queues


Because PHP was not written with multithreading in mind and it would be a huge effort (basically a 100% rewrite) to add it in.

Some would say that PHP is pretty much a gigantic pile: http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/. From that perspective it is not surprising that threading is not included.

We Are All Monica
  • 13,000
  • 8
  • 46
  • 72
  • So...it's just late to the (multithreading) party? That's it? Anticlimactic :| . So why wasn't multithreading support considered in the first place? Lazy Rasmus Lerdorf? – skytreader Aug 27 '12 at 19:25
  • 4
    PHP started as a specific-purpose personal project for Rasmus to track visits to his online resume: http://www.php.net/manual/en/history.php.php. From there it grew into something "other". – We Are All Monica Aug 27 '12 at 19:27
  • 2
    @jnylen This seems to be very bad answer. Multi-threading has been available in PHP since 2012. Check it out [**Pthreads**](http://pthreads.org). Also [**PHP App Server**](http://www.appserver.io) has been built with pthreads. – webblover Feb 08 '14 at 05:46
  • @jnylen I think you can improve your answer. Multithreading is available in PHP through [Pthreads](http://php.net/manual/en/book.pthreads.php) . Please check that. – Md Riadul Islam Feb 28 '19 at 11:44
  • I've learned a lot since 2012. I would still never try to use multithreaded PHP. – We Are All Monica Mar 01 '19 at 01:59
  • "Uniquely among modern languages, PHP was born in a web server. Its strengths are tightly coupled to the context of request-oriented, server-side execution." -- https://slack.engineering/taking-php-seriously-cf7a60065329 – We Are All Monica Mar 01 '19 at 02:00