Stories
Slash Boxes
Comments

SoylentNews is people

posted by cmn32480 on Wednesday November 25 2015, @10:31AM   Printer-friendly
from the get-this-all-sewn-up dept.

As far as I can remember, PHP has always had a terrible reputation at handling very heavy (or asynchronous) tasks. For a long while if you wanted to parallelize long tasks you had to resort to forking through pcntl_fork which had its own issues, and you couldn't really handle the results of those tasks properly, etc.

As such, a habit has kind of developed where we go straight for more intricate solutions such as queuing (which just delays your task if anything), React PHP, or even using another language altogether. But PHP can do threading, and more importantly it's a lot easier than you probably think.

In this article I'm going to dive into the pthreads extension (short for POSIX Threads). It has been around for a while (since 2012) but I feel like too many people forget it exists or assume it is going to be painful to use – mostly because the official documentation is rather slim about it.


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 2) by deimios on Wednesday November 25 2015, @11:44AM

    by deimios (201) Subscriber Badge on Wednesday November 25 2015, @11:44AM (#267954) Journal

    While I share the sentiment some people learned coding with PHP. "When your only tool is a hammer you treat everything as if it were a nail" and all that.

    I wouldn't exclude such people from learning good coding practices, even if the circumstances are not exactly the best. In time they will figure out why their current limited approach is flawed and move on to more suitable languages.

    Starting Score:    1  point
    Karma-Bonus Modifier   +1  

    Total Score:   2  
  • (Score: 3, Interesting) by FatPhil on Wednesday November 25 2015, @12:11PM

    by FatPhil (863) <{pc-soylent} {at} {asdf.fi}> on Wednesday November 25 2015, @12:11PM (#267958) Homepage
    One of my biggest "dafuq?!?!?!" PHP moments came only 2 days ago. I was watching a youtube tutorial on how to do some h/w hacky thing, and the slave got commands from the master by polling a file residing a webserver, whilst the controller was a webpage invoking a PHP script which wrote to that command file. Nothing wrong with that, you say, except that the master was wget/curling "http://myserver/test.php" and test.php contained nothing but whatever is PHP for "return the contents of test.txt". The concept that the slave could just wget "http://myserver/test.txt" had never occured to this programmer who wanted to solve even non-problems with the PHP hammer. And the rest of the code was shitty too.

    However, he had the h/w working, and I don't, no matter how shitty his code is; so he wins, and I suck. (This is why I often will re-do tutorials that I've learnt from, so that others can see what I consider to be cleaner solutions, and not have to fight against head-scratching issues.)
    --
    Great minds discuss ideas; average minds discuss events; small minds discuss people; the smallest discuss themselves
    • (Score: 2) by wonkey_monkey on Wednesday November 25 2015, @01:21PM

      by wonkey_monkey (279) on Wednesday November 25 2015, @01:21PM (#267975) Homepage

      One advantage springs to mind; if he ever decides he wants to provide something more complicated than a plain text file can provide - maybe a different text file at different times of day, or a list of commands generated on-the-fly depending on other server activity - he doesn't have to rewrite the slave.

      --
      systemd is Roko's Basilisk
      • (Score: 2) by FatPhil on Wednesday November 25 2015, @04:21PM

        by FatPhil (863) <{pc-soylent} {at} {asdf.fi}> on Wednesday November 25 2015, @04:21PM (#268042) Homepage
        That can all be done in the PHP script which creates the .txt file from the CGI parameters. An intermediate "here's what he asked for"-to-"here's what we should give him" filter seems overengineering to the extreme. I'm guessing that the guy's only ever executed PHP scripts in his server, and didn't realise you could serve text files too.
        --
        Great minds discuss ideas; average minds discuss events; small minds discuss people; the smallest discuss themselves
    • (Score: 2) by SanityCheck on Wednesday November 25 2015, @02:52PM

      by SanityCheck (5190) on Wednesday November 25 2015, @02:52PM (#268007)

      Hahaha perfect! I seen shit like that too. You would not even believe the piles of turds I stepped int hat were in PHP. I guess I feel vindicated that I reach for python when doing any sort of threaded work. But a lot of code here is in PHP, and IF I have to do use parrelism, I would like to continue with PHP.

    • (Score: 3, Insightful) by Fnord666 on Wednesday November 25 2015, @04:42PM

      by Fnord666 (652) on Wednesday November 25 2015, @04:42PM (#268048) Homepage

      One of my biggest "dafuq?!?!?!" PHP moments came only 2 days ago. I was watching a youtube tutorial on how to do some h/w hacky thing, and the slave got commands from the master by polling a file residing a webserver, whilst the controller was a webpage invoking a PHP script which wrote to that command file. Nothing wrong with that, you say, except that the master was wget/curling "http://myserver/test.php" and test.php contained nothing but whatever is PHP for "return the contents of test.txt". The concept that the slave could just wget "http://myserver/test.txt" had never occured to this programmer who wanted to solve even non-problems with the PHP hammer. And the rest of the code was shitty too.

      Because coding the client software to retrieve and parse a text file results in tightly coupled components. The solution given in the tutorial is significantly more loosely coupled [wikipedia.org] and changes in the server side representation of the data doesn't require matching changes in the client side code. This is a good thing.

      • (Score: 2) by FatPhil on Thursday November 26 2015, @12:05PM

        by FatPhil (863) <{pc-soylent} {at} {asdf.fi}> on Thursday November 26 2015, @12:05PM (#268289) Homepage
        the slave is now tightly coupled to the output of the 1-line php script, and the 1-line php script is tightly coupled to the format of the text file.

        If anything changes, you still need to change 2 endpoints.
        --
        Great minds discuss ideas; average minds discuss events; small minds discuss people; the smallest discuss themselves