Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

edge-iron-py

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

edge-iron-py

Edge-iron-py: run Python and node.js code in-process with edge.js

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

edge-iron-py

Python compiler for Edge.Js using dotnet 6.0.

Differences from edge-py

edge-py only supports .NET Framework 4.x, edge-iron-py supports dotnet 6 and higher.

NOTE This functionality requires IronPython 3.4 and has been tested on Windows only.

Install edge-js and edge-iron-py modules:

npm install edge-js
npm install edge-iron-py
var edge = require('edge-js');

var hello = edge.func('iron-py', function () {/*
    def hello(input):
        return "Python welcomes " + input

    lambda x: hello(x)
*/});

hello('Node.js', function (error, result) {
    if (error) throw error;
    console.log(result);
});
The interop model

Your Python script must evaluate to a lambda expression that accepts a single parameter. The parameter represents marshalled input from the Node.js code. The return value of the lambda expression is passed back as the result to Node.js code. The Python script can contain constructs (e.g. Python functions) that are used in the closure of the lambda expression. The instance of the script with associated state is created when edge.func is called in Node.js. Each call to the function referes to that instance.

The simplest echo Python script you can embed in Node.js looks like this:

lambda x: x

To say hello, you can use something like this:

lambda: x: "Hello, " + x

To maintain a running sum of numbers:

current = 0

def add(x):
    global current
    current = current + x
    return current

lambda x: add(x)
Python in its own file

You can reference Python script stored in a *.py file instead of embedding Python code in a Node.js script.

In your hello.py file:

def hello(input):
    return "Python welcomes " + input

lambda x: hello(x)

In your hello.js file:

var edge = require('edge-js');

var hello = edge.func('iron-py', 'hello.py');

hello('Node.js', function (error, result) {
    if (error) throw error;
    console.log(result);
});
To sync or to async, that is the question

In the examples above Python script was executing asynchronously on its own thread without blocking the singleton V8 thread on which the Node.js event loop runs. This means your Node.js application remains responsive while the Python code executes in the background.

If you know your Python code is non-blocking, or if you know what you are doing, you can tell Edge.js to execute Python code on the singleton V8 thread. This will improve performance for non-blocking Python scripts embedded in a Node.js application:

var edge = require('edge-js');

var hello = edge.func('iron-py', {
    source: function () {/*
        def hello(input):
            return "Python welcomes " + input

        lambda x: hello(x)
    */},
    sync: true
});

console.log(hello('Node.js', true));

The sync: true property in the call to edge.func tells Edge.js to execute Python code on the V8 thread as opposed to creating a new thread to run Python script on. The true parameter in the call to hello requests that Edge.js does in fact call the hello function synchronously, i.e. return the result as opposed to calling a callback function.

See Edge.Js on GitHub for more information.

FAQs

Package last updated on 15 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc