简体   繁体   English

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

[英]Passing data between running PHP scripts

For multiple running PHP scripts (10 to 100) to communicate, what is the least memory intensive solution? 要使多个运行中的PHP脚本(10到100个)进行通信,什么是最少内存占用的解决方案?

  • 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) 共享内存(APC或核心功能)
  • Message queues (Active MQ and company) 消息队列(Active MQ和公司)

This is hard to answer without knowing: 不知道就很难回答:

  • How much data will they send in each message (2 bytes or 4 megabytes)? 他们将在每个消息中发送多少数据(2字节或4兆字节)?
  • 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. 在PHP环境中,使用数据库可能是最容易设置的,并且取决于每分钟有多少个查询和这些查询的类型,这实际上可能是最明智的解决方案。 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. 但是,同样,如果没有有关该应用程序的更多信息,很难确定。

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. 该记录将包含唯一的ID,收件人和消息。 The recipient script can easily poll for new messages, and after reading, quickly and safely remove the record in question. 收件人脚本可以轻松轮询新邮件,阅读后可以快速安全地删除有问题的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM