简体   繁体   中英

Calling a Python module from Perl

I created a module in Python which provides about a dozen functionalities. While it will be mostly used from within Python, there is a good fraction of legacy users which will be calling it from Perl.

What is the best way to make a plug in to this module? My thoughts are:

  1. Provide the functionalities as command line utilities and make system calls
  2. Create some sort of server and handle RPC calls (say, via JSON RPC)

Any advise?

One other choice is to inline Python directly in your Perl script, using Inline::Python .

This may be simpler than other solutions, and only requires one additional module.

In the short run the easiest solution is to use Inline::Python. Closely followed by calling a command-line script.

In the long run, using a server to provide RPC functionality or simply calling a command-line script will give you the most future proof solution.

Why?

Becuase that way you aren't tied to Perl or Python as the language used to build the systems that consume the services provided by your library. Either method creates a clear, language independent interface that you can use with whatever development environment you adopt.

Depending on your needs any of the presented options may be the "best choice". Depending on how your needs evolve over time, a different choice may be revealed as "best".

My approach to this would be to ask a couple of questions:

How often do you change development tools. You've switched to Python from Perl. Did you start with Tcl and go to Perl? Are you going to switch to the exciting new language X in 1, 5 or 10 years? If you change tools 'often' (whatever that means) emphasize cross tool compatibility.

How fast is fast enough? Is the start up time for command line solutions ok? Does Inline::Python slow things down too much (you are still initializing a Python interpreter, it's just embedded in your Perl interpreter)?

Based on the answers to these questions, I would do the simplest thing that is likely to work .

My guess is that means in order:

  1. Inline::Python
  2. Command line scripts
  3. Build an RPC server

Provide the functionalities as command line utilities and make system calls

Works really nicely. This is the way programs like Python (and Perl) are meant to use used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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