2

I've got a version of the A* algorithm that builds a graph of the UK road and cycle network in Python lists. It takes about 30 seconds to initialise, but once done can very quickly find the shortest route between any two vertices. The start and finish vertex ids are provided by PHP.

I'm trying to work out the best way of communicating between PHP and the Python program. I only want to do the initialisation phase when the Apache server starts, so my question is: How do I keep the python program alive and request routes from it via php? I have a GLAMP setup.

Simon Nuttall
  • 394
  • 1
  • 3
  • 12
  • @Pekka Presumably GNU/Linux as opposed to just Linux, as GNU/Linux is the technically correct name for an OS with the Linux kernel and GNU subsystems. – rossipedia Jun 24 '10 at 23:07
  • @Bryan aaah, cheers. @Simon that is one impressive system you're building there! **Wow**. What kind of data are you using for this? Will there be a version for the Cologne area in the conceivable future? :) – Pekka Jun 24 '10 at 23:12
  • Please, please, please. Use search. – S.Lott Jun 24 '10 at 23:22
  • possible duplicate of [Calling Python in PHP](http://stackoverflow.com/questions/166944/calling-python-in-php) – S.Lott Jun 24 '10 at 23:22

4 Answers4

2

Easiest way that I can think of would be XMLRPC. Python makes it horribly easy to set up an XMLRPC server, and there's php_xmlrpc for bindings on the PHP side...

def calculate_path(v1, v2):
  return [v1, ..., v2]

from SimpleXMLRPCServer import SimpleXMLRPCServer
server = SimpleXMLRPCServer(('localhost', 9393))
server.register_function(calculate_path)
server.serve_forever()

and you're running and should be able to do an XMLRPC call for calculate_path on http://localhost:9393/ from your PHP app.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Thanks for all the solutions offered, but I went for this one in the end as it looked the simplest. However, it did take me quite a while to get the Pear module working with PHP5.3,- which I did by predefining function dl() {return false;} We've now stripped out most of the XMLRPC package to have a very lightweight version. – Simon Nuttall Jul 01 '10 at 10:01
0

Check this question out: Calling Python in PHP

Also check out this extension: http://www.csh.rit.edu/~jon/projects/pip/

I don't know if you're hosting environment allows you to add new php extensions, but you get the idea.

Community
  • 1
  • 1
rossipedia
  • 56,800
  • 10
  • 90
  • 93
0

I would make a "backend" server from your Python application. There are many ways to call into the Python application:

This avoids any startup penalty for the Python application.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
0

You could do something as simple as a REST web server in Python using web.py:

http://webpy.org/

and then call that via PHP, should make the whole task super simple.

See this for more info:

http://johnpaulett.com/2008/09/20/getting-restful-with-webpy/

Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150