Sign in to follow this  
JensB

[web] Can PHP do 'persistent processes'?

Recommended Posts

A quick question that I don't know where to turn to to get the answer for: Can I, using PHP on a Apache - Create a persistent process that updates a database? Ie, I have a web site that runs using PHP that will display the state of the database - But, I also want a 'ticking, persistent process' that continuously runs and updates that database. I suspect the answer is no, and that I would have to write that as a Java program or something - But kinda want it confirmed. Thanks!

Share this post


Link to post
Share on other sites
I'm no PHP expert (at all), but this is something one could easily achieve on a *NIX-like system with cron(8) and a script written in PHP, Python, Perl, ... Unless you have very specific needs, there's probably no reason to write a daemon just for updating a database once in a while.

I'm sure Windows features a scheduler and, in case it did not fit your requirements, I know there are various implementation of cron for Windows.

Mac OS X also has a cron scheduler.

Share this post


Link to post
Share on other sites
Yes you can write "regular" applications in PHP, just like any other language.
In fact, I've worked at a company that used PHP to extract game assets from a database and compiled them into game-archive formats ;)

However, you probably don't want to start this PHP application through apache, you should start the script running yourself by running "php.exe c:/yourfile.php" etc...
On a windows-based server you could configure this as a scheduled task and on *nix servers you could use a cron job.


Also, PHP normally imposes a time limit on how long a PHP application can execute for (because usually a long execution time means that a web-page is broken), so make sure to read up on the set_time_limit function.

Share this post


Link to post
Share on other sites
There are several ways to give your php-website a more application like feel. One solution frequently used is the XMLHttp object. It allows you to send and receive data from a server without constantly having to reload a page. By calling the object at frequent intervals through javascript you can update your page dynamically with content from a database and vice versa.

Share this post


Link to post
Share on other sites
That said, PHP's focus is on web development. There are things that PHP isn't really good at such as threading and interprocess communication. There are also memory issues with long-running processes.

If none of that applies to you then have a blast. There are even GTK bindings for PHP so you can write desktop GUI applications. But you may want to give Python a spin. It's much better suited at such tasks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sander
That said, PHP's focus is on web development. There are things that PHP isn't really good at such as threading and interprocess communication. There are also memory issues with long-running processes.

If none of that applies to you then have a blast. There are even GTK bindings for PHP so you can write desktop GUI applications. But you may want to give Python a spin. It's much better suited at such tasks.


you saying phyton is better at the persistent thing I'm trying to do? (memory is an issue indeed) -- or are you saying phyton is better at gui?

Share this post


Link to post
Share on other sites
It depends on the type of application you're writing. PHP is perfect for writing things that you execute at the commandline. I.e. things that you start (either manually from the commandline or from a cron job) then do a certain task and then exit when the task is done. However, if you are planning to write a daemon process (i.e. something that runs in the background all the time and communicates with other processes) then you had better look at Python or Perl (or plain old C/C++).

Python is just really easy and fun to work with, but it's quite different from PHP. If all you know is PHP then it will take some time to getting used to, but it will be a fun ride. Once you get used to the idea of using whitespace instead or curly braces to mark blocks of code it's really an elegant language.

Perl is a lot more like PHP but has a significant amount of quirks (just like PHP). If you only know PHP it's probably easier to get going in Perl but it's harder to master in the end. The lack of proper Object Orientation is a drawback.

I would recommend against using C/C++ for any daemon processes unless you're an expert in those languages.

Share this post


Link to post
Share on other sites
Another reason to use the PHP CLI is if you already have a significant amount of basecode written in PHP. For instance, i have tons of PHP code that runs a commercial website, plus all the installed libraries and tools that it uses. If i just want a script that connects to the database, gets some info, and emails a report somewhere, of course i'm going to use PHP. I've already got all the classes and tools to easily do things in PHP.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this