在运行PHP脚本之间传递数据

For multiple running PHP scripts (10 to 100) to communicate, what is the least memory intensive solution?

  • Monitor flat files for changes
  • Keep running queries on a DB to check for new data

Other techniques I have heard of, but never tried:

  • Shared memory (APC, or core functions)
  • Message queues (Active MQ and company)

In general, a shared memory based solution is going to be the fastest and have the least overhead in most cases. If you can use it, do it.

Message Queues I don't know much about, but when the choice is between databases and flat files, I would opt for a database because of concurrency issues.

A file you have to lock to add a line to it, possibly causing other scripts to fail to write their messages.

In a database based solution, you can work with one record for each message. The record would contain a unique ID, the recipient, and the message. The recipient script can easily poll for new messages, and after reading, quickly and safely remove the record in question.

This is hard to answer without knowing:

  • How much data will they send in each message (2 bytes or 4 megabytes)?
  • Will they run in the same machine? (This looks like a yes, else you wouldn't be considering shared memory)
  • What are the performance requirements (one message a minute or a zillion per second)?
  • What resource is most important to you?

And so on...

Using a DB is probably easiest to setup in a PHP environment and, depending on how many queries per minute and the type of those queries, that might indeed be the sanest solution. Personally I'd try that first and then see if it's not enough.

But, again, hard to tell for sure without more information on the application.