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

I am building an application in C that takes in requests and, depending on the request, sometimes calls out to Perl. More importantly, the Perl code is allowed to make calls back to the application just like any other application. In other words, I need multiple Perl interpreters from my C program.

I was hoping someone might have a more involved example than perlembed which involves creating a new interpreter on request and closing it down when we're done. Even better, an example of maintaining a group of persistent interpreters that each have independent locks would be *extremely* helpful.

Thanks much.

Replies are listed 'Best First'.
Re: Spawning multiple interpreters from C
by redhotpenguin (Deacon) on Dec 14, 2004 at 21:18 UTC
    Have you thought about building your application as an Apache module, then having it alter the request and use mod_perl for the Perl interpreter?

    Seems like it would save a great deal of work and allow you to concentrate on your application instead of the framework.

      This is especially attractive under mod_perl 2 where you can write protocol handlers.
Re: Spawning multiple interpreters from C
by fglock (Vicar) on Dec 14, 2004 at 21:52 UTC

    I don't see why you need multiple Perl interpreters. Isn't is possible to call a single interpreter multiple times?

    About the persistent interpreters - You may want to start multiple Perl processes and call them using IPC (see Net::Daemon), but than you wouldn't be able to use C callbacks.

Re: Spawning multiple interpreters from C
by superfrink (Curate) on Dec 15, 2004 at 06:30 UTC
    I might not understand what exactly is going on but it sounds like your system of calling perl to do things is not reentrant.

    Are you locking anything or would you be making anything available to be corrupted if you allowed multiple calls to perl?

    I'm not sure what else would be a problem. What exactly do you mean by "calls"? Are any new child processes being created? Are the calls back to the C program made back to one process or is the C program run again (so there are multiple instances of it running)?
      The C process is a daemon that accepts web services queries. It has a number of providers that it respond to these queries, some of which are in C and some of which are in Perl. For the Perl providers, I have a persistent interpreter at the moment. The issue is that some of the Perl providers make web services calls to the daemon as well, which needs to call out to a Perl provider to return the data. Since the original Perl provider is still locking up the interpreter, it just hangs, waiting for a response.