2

Does anyone have an idea if there is already a zmq module for apache? If there is please share the link or any reference. My scenario is as follows:

Server Config:

  1. Apache 2.4.12, with prefork
  2. PHP 5.5
  3. ZMQ 4.0.X

My problem is, whenever I try to create a zmq socket(pub) connection from my application to a separate service (SUB) with a streamer device in between, it creates a new socket everytime the application is initialized as my apache is in prefork mode, creating new instance(child) on every request. How can I create a single context/socket where any number of PHP requests from subsequent apache child processes can send data to the socket, which will avoid the creation of multiple sockets and exhausting the system resources. This will also, I believe, reduce the overhead caused due to creation of new sockets and make it faster.

As an alternative is it possible to create an apache module, whose functions and resources I can access from PHP application and use it to just send data, where the context and socket are created only once, and are persistent, during apache load.

Jason
  • 13,606
  • 2
  • 29
  • 40
  • So basically you want a connection pool that PHP can use, something similar to what it does with MySQL connections? – N.B. Jul 24 '15 at 13:41
  • yes !! as in .. it just create a context and socket and i stream data to it !! its similar but not same !! – Mrigesh Pokhrel Jul 24 '15 at 18:31

1 Answers1

1

Short answer - you can't. Your problem in this is Apache and how it works - it shuts down the PHP process after request finishes. Also, you can't share a context or a socket created in an Apache process between PHP processes.

I don't know what you're trying to do or why you even exhaust system resources (quite odd), but if I were you I'd use a more advanced server that uses ZeroMQ internally for its transport layer: Mongrel2. You could create a PHP extension, serve PHP via FPM and then have Apache proxy requests to your PHP-FPM which can then pool the already existing ZMQ connections. However, I would expand the question with how the resources are exhausted that fast.

If that's all too much, then you can consider this:

  • PHP processes spawned by Apache accept the data and fill some sort of storage (database, file, shared memory)
  • Once the makeshift-queue has been populated, before exiting the PHP scripts raise SIGUSR2 for a daemon process which reads the queue
  • You have a daemon running that reads the queue, activates upon SIGUSR2 and sends the data via ZMQ socket - you have a single process that uses ZMQ now and multiple PHP processes that interact with it

Since your requirement is a bit unclear, it's quite possible that all I wrote is for nothing so if you can expand your question just a little bit with more info.

N.B.
  • 13,688
  • 3
  • 45
  • 55