July 11th, 2013, 06:34 PM
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.
July 11th, 2013, 06:42 PM
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.
July 11th, 2013, 11:18 PM
Ah, same library? That'd be cool.
Originally Posted by E-Oreo
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.
July 12th, 2013, 12:21 AM
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 ...
July 12th, 2013, 12:25 AM
Originally Posted by krakjoe
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.
July 12th, 2013, 01:16 AM
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 ...
July 12th, 2013, 10:39 AM
Originally Posted by krakjoe
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.