suaveant has asked for the wisdom of the Perl Monks concerning the following question:

My company uses FastCGI, which has been annoying me, so I have written my own version of FastCGI entirely in perl. A daemon is created, children are spawned, calls come in with the proper environment and output is stuffed back through a socket, relatively simple stuff.

Now, what I am wondering; Is there a better way to do it?

Obviously, for separate servers, sockets are still the way to go. But we have fcgis that we call for data, which are usually on the same system. Is there a better way than a socket to make calls of unknown size on a local system? I have tried UNIX domain sockets, which seem to improve the connection some, but nothing remarkable.

Also, just using sockets I have been playing around. One way to connect is through a C program that you call through the shell, another is a perl module that does a pure perl connect, and the third is to wrap the C code from the program in a SWIG module. I have done all three, and they seem to have improved speed respectively, however... the perl wrapped C module takes the same time in real seconds as the pure perl module, but less system time (according to Benchmark). Am I missing something? Unless there is a noticable difference I am more inclined to travel the pure perl route.

Does anyone have any suggestions as to other approaches I might investigate? I'm running low on ideas...

                - Ant
                - Some of my best work - (1 2 3)

Replies are listed 'Best First'.
Re: Fastest way to talk to a perl daemon
by trs80 (Priest) on Aug 06, 2002 at 17:59 UTC
      I will take a look, though I can't really use it directly, we have a bunch of FCGIs and I'm not sure I could sell the others on completely changing the way they work :) My code pretty closely mimics FCGI, and adds management. But I will check to see his method of local connections...

                      - Ant
                      - Some of my best work - (1 2 3)

Re: Fastest way to talk to a perl daemon
by kvale (Monsignor) on Aug 06, 2002 at 17:42 UTC
    Another fast method of sharing data is the System V IPC shared memory setup. Look at  shmctl(), shmget(), shmread() and <code> shmwrite(). <code>

    -Mark

      I had thought of that, but what I remember of shared memory from school, I'm not sure how to apply that to this situation. Multiple children run, so to have them all available they would either have to share the same memory space (which is limited in size, if I recall), or each have their own segment of memory, in which case you would still probably have to connect via a socket to identify, losing your speedup in most cases, probably.

      I suppose maybe they could have a pooled memory space and individual... and identify calls through the pooled space, I'm not familiar enough with it.

      The way the system currently works is that a master listen socket is created, which is then listened on by all the children. I'm not sure how to handle the calls with the shared memory, not that I am saying it couldn't be done, I just am not sure how it would...

                      - Ant
                      - Some of my best work - (1 2 3)

Re: Fastest way to talk to a perl daemon
by Zaxo (Archbishop) on Aug 07, 2002 at 05:20 UTC

    Unix pipe is the most primitive and often the best method of ipc. I just posted, at Many-to-One pipe, a demo taking advantage of file descriptor duplication in fork to allow many children to talk to one over a pipe.

    Update: suaveant, you won't be able to use that for processes which are not forked from a common parent. I was picturing a server daemon forking a helper process, possibly with another pipe back to the parent, and then creating little connection servers which all feed the helper.

    After Compline,
    Zaxo

      Although, since the calls coming in are from other process groups, the only real way to do this would be either a file pipe, or to open the pipe directly somehow using file handle passing, I believe. I know pipes are good for parents and children, but how do they really work for talking to a daemon's children?

                      - Ant
                      - Some of my best work - (1 2 3)