views:

154

answers:

4

I'm assuming that for every page request, the webserver (eg. Apache) creates a new instance of a script in memory. Can these instances communicate with each other while running? and pass data too?

+3  A: 

If you want to pass data between scripts in PHP I suggest using either memcached or a database. Or possibly APC.

If the scripts belong to the same session, they could theoretically communicate via the session but this would be effectively a one-way communication in most cases because only one script can access the session at any one time (session_start() locks the session until that script ends the session implicitly or explicitly).

cletus
+1  A: 

The model that PHP scripts operate off of doesn't really contain the notion of any kind of persistence in memory for those scripts, since generally they're designed to only run for the minimum of time required to serve the requested page. This would make it hard to have any meaningful use for stateful communication between those scripts, since typically once the page is served there's nothing more for the script to do. Thus usually any communication between PHP scripts is done more through manipulation of database entries and the like.

If you have some sort of continual processing that should be happening for which you'd want to be passing data around, you might want to look into other web application models such as servlets.

Amber
Good stuff, but a lot of lecturing which is attempting to deviate from a good answer!
Jenko
+1  A: 

You should be able to do this with some shared memory, as described here: http://blog.taragana.com/index.php/archive/how-to-use-shared-memory-in-php/ (assuming you're not running on Windows)

Martin
+2  A: 

I believe Martin and Cletus' suggestions are valid. My choice would be function of the end goal of the script.

  • How much data will you be throwing around? Can you handle the overhead of an external process?
  • What kind of data are you exchanging? Is it normalized? Or is it now worth normalizing?
  • Will you need to refer to that data later on? Or can it be discarded after being processed?
  • Will those scripts ever run on different servers?

  • Flat files, with a locking mechanism

  • Relational DB
  • Document DB (key/value store, whether persistent or not)
  • Shared memory (APC, or core functions)
  • Message queues (Active MQ and company)

I think you'll get the most value by externalizing the process as you can have more than one machine managing the messages/data and more than one producing/consuming them.

Leprechaun