Can instances of the same PHP script cross communicate?

对着背影说爱祢 提交于 2019-12-11 04:48:50

问题


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?


回答1:


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).




回答2:


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.




回答3:


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.




回答4:


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)



来源:https://stackoverflow.com/questions/1430417/can-instances-of-the-same-php-script-cross-communicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!