librelist archives

« back to archive

Photon, PHP framework for Mongrel2

Photon, PHP framework for Mongrel2

From:
Loic d'Anterroches
Date:
2011-01-17 @ 20:57
Hello,

short, if you just happen not to like PHP, just skip, if you are 
curious, just go ahead.

So, I created a small framework for Mongrel2 in PHP. What you get is 
basically an application server in PHP which is single process, single 
thread. It scales by starting more processes. And it is:

- fast.
- a long term PHP process, so no need for a bytcode cache.
- on memory diet with 2MB of memory per process (when not handling a 
request).
- robust, you can pour in a 200MB POST request and it will magically 
swap it and only parse on demand the POST fields.
- easy to control from the command line (using a zmq control socket).
- automatically sign your cookies to trust them for message passing 
(sha1 hmac).
- does not require a database.

Example of usage:

$ photon init myproject
$ cd myproject
$ photon server start
$ curl http://localhost:6767/handlertest/foo
Hello World!
$ photon server list
Waiting for the answers...
Photon id                     Uptime        Served  Mem. (kB)  Peak mem. 
(kB)
-------------------------------------------------------------------------------
loa-desktop-29874-1295296119  0d00:00:40    1       1517       1928
-------------------------------------------------------------------------------
1 Photon servers running. Memory usage: 1517kB.
$ photon server start
$ photon server listWaiting for the answers...
Photon id                     Uptime        Served  Mem. (kB)  Peak mem. 
(kB)
-------------------------------------------------------------------------------
loa-desktop-29874-1295296119  0d00:01:24    1       1517       1928
loa-desktop-29896-1295296185  0d00:00:18    0       1352       1362
-------------------------------------------------------------------------------
2 Photon servers running. Memory usage: 2870kB.
$ photon server stop
Waiting for the answers...
Photon id                     Answer
---------------------------------------
loa-desktop-29896-1295296185  KO
loa-desktop-29874-1295296119  KO
---------------------------------------
2 Photon servers stopped.

Source: http://projects.ceondo.com/p/photon/
List:   photon.users@librelist.com

# Some Q/A

When announced on my blog, I have received a bit of heat, here are some 
of the questions and answers.

1. Why PHP?
The interest of using PHP is that, basically, PHP is just a glue code 
over C libraries. This is why it is really really fast.

2. Why another framework in PHP?
All the current frameworks are designed to work with one request/one 
answer model. You can get the request from Mongrel2 and inform a python 
server through zmq to send the answer. I wanted something with minimal 
memory foot print too.

3. PHP sucks at memory management, long running processes with PHP is a 
dead end.
PHP as fastcgi already restarts each child after 500 requests, the PHP 
interpreter is extremely fast to start, just get your Photon processes 
to die after n +/- rand(n/10) requests and be restarted by your process 
manager. You can also let them die if a memory threshold is reached 
after the processing of a request.

4. For which usage?
For the fun of playing with Mongrel2 and zeromq, but especially for 
Cheméo. Cheméo is a chemical property search engine and model 
devolpment. Models are regressed using Python + R, so it will be 
possible to have some small services powered with zeromq and get Photon 
to communicate with them. http://www.chemeo.com

The doc folder is still just a brain dump, not up-to-date, the code is 
early alpha code. But for people interested in PHP+Mongrel2, you can 
have some fun.

Source: http://projects.ceondo.com/p/photon/
List:   photon.users@librelist.com

loïc

--
Cheméo, High Quality Chemical Properties - http://www.chemeo.com
Indefero - Project management and code hosting - http://www.indefero.net
Céondo Ltd - Web + Science = Fun - http://www.ceondo.com

Re: [mongrel2] Photon, PHP framework for Mongrel2

From:
Zed A. Shaw
Date:
2011-01-18 @ 02:41
On Mon, Jan 17, 2011 at 09:57:46PM +0100, Loic d'Anterroches wrote:
> Hello,
> 
> short, if you just happen not to like PHP, just skip, if you are 
> curious, just go ahead.

That's awesome.  I may have to delve into this and see what I can steal
for Tir.

> # Some Q/A
> 
> When announced on my blog, I have received a bit of heat, here are some 
> of the questions and answers.

Pfft, the best projects do this. :-)

> 2. Why another framework in PHP?
> All the current frameworks are designed to work with one request/one 
> answer model. You can get the request from Mongrel2 and inform a python 
> server through zmq to send the answer. I wanted something with minimal 
> memory foot print too.

This is what I tell folks, that current frameworks assume a strict
req/resp pattern and don't let you do async.

The other answer I give is: "Mongrel2 handles about 90% of the stuff
other frameworks replicate, so you end up having a smaller framework to
deal with.  Basically, Mongrel2 turns every framework in to a
micro-framework."

> 3. PHP sucks at memory management, long running processes with PHP is a 
> dead end.

Is there a particular reason for this?

> devolpment. Models are regressed using Python + R, so it will be 
> possible to have some small services powered with zeromq and get Photon 

Take a look at how I did tasks in Tir.  They start up just like
handlers, but it's expected that you'll pass things to them from a
handler, rather than them talking to mongrel2.  It makes glueing
different libraries together and offloading work really easy.

> Source: http://projects.ceondo.com/p/photon/
> List:   photon.users@librelist.com

Very cool, I'll check it out later and see what it's like.

-- 
Zed A. Shaw
http://zedshaw.com/

Re: [mongrel2] Photon, PHP framework for Mongrel2

From:
Loic d'Anterroches
Date:
2011-01-18 @ 08:15

On 2011-01-18 03:41, Zed A. Shaw wrote:
>> short, if you just happen not to like PHP, just skip, if you are
>> curious, just go ahead.
>
> That's awesome.  I may have to delve into this and see what I can steal
> for Tir.

I think the moste interesting part is the way to control the processes 
through zeromq. The rest is just as you wrote, a micro framework.

>> # Some Q/A
>>
>> When announced on my blog, I have received a bit of heat, here are some
>> of the questions and answers.
>
> Pfft, the best projects do this. :-)
>
>> 2. Why another framework in PHP?

[...]

> The other answer I give is: "Mongrel2 handles about 90% of the stuff
> other frameworks replicate, so you end up having a smaller framework to
> deal with.  Basically, Mongrel2 turns every framework in to a
> micro-framework."

Effectively, Mongrel2 is doing the bulk of the job, so it is very easy 
to just plug in and do the minimal amount of work to pass the request to 
the right piece of code to figure out what to do with.

>> 3. PHP sucks at memory management, long running processes with PHP is a
>> dead end.
>
> Is there a particular reason for this?

For historical reasons, PHP is using a request/reply approach with a 
hard reset of all the memory after each. It is only with PHP 5.2 (maybe 
5.3) that a more efficient garbage collector has been created to clean 
the memory while the script runs. But, as the framework is very small, I 
create only a minimal number of objects, I clean after myself (open 
files etc.) so this is not really a problem. The PHP is nicely going up 
during the handling of the request and down again to the minimum while 
waiting for the next incoming request.

>> devolpment. Models are regressed using Python + R, so it will be
>> possible to have some small services powered with zeromq and get Photon
>
> Take a look at how I did tasks in Tir.  They start up just like
> handlers, but it's expected that you'll pass things to them from a
> handler, rather than them talking to mongrel2.  It makes glueing
> different libraries together and offloading work really easy.

I will check that, my idea was to kind of replicate a bit what is also 
available with the Yahoo Pluton project. From PHP, this would mean 
something like:

$task1 = new Task1();
$task2 = new Task2();
$manager = new TaskManager();
$manager->add($task1);
$manager->add($task2);
list($ans1, $ans2) = $manager->run(50);

The run would dispatch the requests and wait a maximum of 50 ms for the 
answers. If not there, then $ans1 or $ans2 would be null.

If we can get a simple protocol like for the Mongrel2 <-> Handler 
protocol for this kind of inter tasks communication, that would be great.

>> Source: http://projects.ceondo.com/p/photon/
>> List:   photon.users@librelist.com
>
> Very cool, I'll check it out later and see what it's like.

Thanks!

By the way, small bug: http://librelist.com/browser/mongrel2/
I cannot see the messages for 2011.

loïc

--
Cheméo, High Quality Chemical Properties - http://www.chemeo.com
Indefero - Project management and code hosting - http://www.indefero.net
Céondo Ltd - Web + Science = Fun - http://www.ceondo.com