#1
  1. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127

    Pthreads & websockets


    So I think I found my next adventure.

    I've known about the coming of Websockets for a while but haven't looked too deeply into them until noticing some Disqus behaviors today, which use either Websockets or Long Polling.

    But I opted to look at Websockets specifically.

    And having made a PHP-based IRC bot before, I'm painfully aware of the single-thread limitations of any constantly-running PHP process that's servicing requests.

    So I found my way to pthreads.

    Unfortunately I'm limited to research while at work but I'll be hitting the code tonight, looking at them. Until then and besides my personal quest, has anyone else dabbled with either or both of these technologies in PHP?

    P.S. Yes I know multithreading is against the PHP grain, but I like to push the envelope. And so far pthreads sounds fairly legit.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    I've used pthreads in C, but not in PHP. It's very well established in general but not commonly used in PHP. I've used pcntl to fork processes in PHP before, but it was a while ago and I can't remember what I was using it for.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    Originally Posted by E-Oreo
    I've used pthreads in C, but not in PHP. It's very well established in general but not commonly used in PHP. I've used pcntl to fork processes in PHP before, but it was a while ago and I can't remember what I was using it for.
    Ah, same library? That'd be cool.

    The nice thing is that my system is already kinda persistent/thread-capable. At least in theory. So I can play with this without having to change that much of it.

    It should be neat to play with, though. The end-game would be to funnel all HTTP activity through very simple frontend scripts that then send the request through WebSockets to a multithreaded, persistent PHP service. Mostly just to see if it works. In theory that'd remove some of the overhead of rebuilding the same objects over and over again, for every page load.

    But we'll see. That's quite a bit different than your usual PHP web app.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0
    The only common ground between pthreads and Posix Threads is their shared name. As you have found out, using pthreads in PHP is much like using threads in any other high level language around and nothing at all like using Posix Threads in C.

    I'm interested to know how you got on with pthreads last night, if your expectations were met, or broken ... do tell ...
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    Originally Posted by krakjoe
    The only common ground between pthreads and Posix Threads is their shared name. As you have found out, using pthreads in PHP is much like using threads in any other high level language around and nothing at all like using Posix Threads in C.

    I'm interested to know how you got on with pthreads last night, if your expectations were met, or broken ... do tell ...
    Definitely.
    It'll be a while yet, though. Tonight ended up being spent fixing some applications and finally installing thread support. I won't be actually playing with it until tomorrow night now.

    I haven't dealt with threads in any other language, though, so my views won't offer much in the way of a comparison.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0
    You shouldn't need to have prior experience or knowledge in order to simply do two things at once; I mean with all that is possible in the world ... off the top of my head, Will. I. Am had a song played from mars, face transplants are possible, and you can have cameras installed in place of your eyes ... doing two things at once isn't a very tall order, is it !!

    So, just as interested in your findings ...
  12. #7
  13. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    Originally Posted by krakjoe
    You shouldn't need to have prior experience or knowledge in order to simply do two things at once; I mean with all that is possible in the world ... off the top of my head, Will. I. Am had a song played from mars, face transplants are possible, and you can have cameras installed in place of your eyes ... doing two things at once isn't a very tall order, is it !!

    So, just as interested in your findings ...
    lol

    Well, it's not the "two things at once" that I'll have to learn. My app uses the Multiton pattern and I'd want the instantiated objects to be available across threads, so that's what'll be new for me. And then figuring out how to safely handle updates to those objects (ie., locking and such).

    My use of the pattern is further augmented by a little cheating: using xdebug_debug_zval to figure out the refcount of objects, for the purpose of eventually recycling those that are not longer actively used. If anything about threading breaks that, it might be a nonstarter.

IMN logo majestic logo threadwatch logo seochat tools logo