Welcome Guest, Not a member yet? Register   Sign In
Advice on writing Daemon in PHP/CI or ?
#1

(This post was last modified: 03-05-2018, 05:21 AM by richb201.)

I have finished my setup program which uses CI and Grocery_Crud to populate some tables. I am also 90% done with my Chrome Extension. I need to write a daemon that will run on the server and has two functions 1) receive a json buffer from the extension via ajax, look up a key value with some small SQL query and return a buffer of data and sedn it back to the extension 2) receive a json buffer and append it to a table. This is pretty much the heart of the system, so i do need to handle these requests and responses fairly quickly. 

I know how to do this in C in a single process; I'd spawn the process and wait (ie blocking) on a buffer. But I get the feeling that PHP on Apache runs differently. What is the best architecture for such a thing? Is there any code I can use as an example? If not can someone point to a document that discusses how to handle a daemon in this environment? Or should I be writing it in C? By definition this daemon has no UI. 

thx in advance
proof that an old dog can learn new tricks
Reply
#2

An AJAX request is (roughly) equivalent to spawning a new process on the server. Because CI is already in place it seems to me that using it would greatly shorten the development time and still be reasonably lightweight/efficient. From my point of view (as an old C programmer) writing a daemon would be a lot more effort than writing a controller and model combo to respond to an ajax request.

Another idea (possibly nearly as development intensive as a daemon in C) would be to use Node.js to process javascript on the server side. Node has a built-in HTTP module that listens to server ports and gives a response back to the client. It should be lighter weight and faster than apache/PHP and easier than writing a daemon.
Reply
#3

Hey Dave. I guess what I most concerned with about using PHP/CI is the spawning. Does each time an ajax message comes into the server, a brand new copy of the PHP code (ie, a process) is spawned? Thus this is a multiprocess solution, which might be OK, except that I'd be concerned about the time it takes to load a new copy of the process from the disk. Thanks for bringing a little sanity into my plan!
proof that an old dog can learn new tricks
Reply
#4

(03-05-2018, 10:13 AM)richb201 Wrote: Hey Dave. I guess what I most concerned with about using PHP/CI is the spawning. Does each time an ajax message comes into the server, a brand new  copy of the PHP code (ie, a process) is spawned? Thus this is a multiprocess solution, which might be OK, except that I'd be concerned about the time it takes to load a new copy of the process from the disk. Thanks for bringing a little sanity into my plan!

Check out this page for some details and further reading. But the main point to keep in mind is that Apache is designed to handle multiple requests effciently.

As to "loading a new copy" I would expect it's done by copying from server modules already in memory. Or, at the worst, a new thread is given access to loaded in-memory modules and access is managed via some thread synchronization primitives. But I've never dug deeply into Apache internals so don't take my word for it.
Reply
#5

(03-05-2018, 10:13 AM)richb201 Wrote: Hey Dave. I guess what I most concerned with about using PHP/CI is the spawning. Does each time an ajax message comes into the server, a brand new  copy of the PHP code (ie, a process) is spawned? Thus this is a multiprocess solution, which might be OK, except that I'd be concerned about the time it takes to load a new copy of the process from the disk. Thanks for bringing a little sanity into my plan!

An AJAX message is just a regular HTTP request that's hidden by the browser and executed behind the scenes. And yes, each request spawns a new PHP process. In this scenario, Apache is your deamon.

But that shouldn't be much of a concern - most of the internet runs like that and it works just fine.

I would recommend nginx+php-fpm for higher performance and overall a better environment (people are so used to Apache that few have a reason to look for alternatives, but nginx is objectively a superior httpd). In that scenario, php-fpm will be the deamon spawning workers and unlike Apache+mod_php, it actually has all PHP extensions preloaded, so that's a significant boost.

If you really want to write your own deamon in PHP, I'd look into Amp, Aerys and/or ReactPHP - those are basically the PHP answer to Node.js.
Reply
#6

Thanks Narf! The plan is to ultimately host this thing on Google Cloud services. So it will not be running on apache. Does that modify your excellent response? Is there a way to run jinx locally for development?
proof that an old dog can learn new tricks
Reply
#7

Of course you can run nginx locally, but I have no experience with Google Cloud, so I can't comment on that.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB