Giter Site home page Giter Site logo

pytalk's Introduction

pytalk.js

Build Status

Pytalk is a module for bidirectional communication between Node and Python.

It lets you create Python process, and communicate with it via standard streams. Every message passed through pytalk gets serialized into JSON. Before starting the process, Pytalk modifies the Python code, instantiating an event loop and allowing you to send and recieve messages with pytalk_emit, pytalk_on or registering Python method with pytalk_method.

Fork edits:

OG NPM package (github repo was removed)

This fork allows you to run python scripts as root by passing the option root

let worker = pytalk.worker('worker.py', {sudo: true});

Install

Install through npm

npm install pytalk

Usage

  1. Using a worker script, calling registered Python method asynchronously
index.js                                      |  worker.py
------------------------------------------------------------------------------------------------
const pytalk = require('pytalk');             |  import cv2
                                              |  import uuid
                                              |  
let worker = pytalk.worker('worker.py');      |  @pytalk_method('blur')
let blur = worker.method('blur');             |  def cv2blur(path):
                                              |      img = cv2.blur(cv2.imread(path), (20, 20))
blur('image.jpg', (err, blurred) => {         |      dst = str(uuid.uuid1()) + '.jpg'
  console.log(`Saved to ${blurred}`);         |      cv2.imwrite(dst, img)
});                                           |      return dst
  1. Importing modules using proxy PyObjects.
const pytalk = require('pytalk');

let worker = pytalk.worker();      // Create Python process

let math = worker.import('math')   // Load modules
  , os   = worker.import('os')
  , np   = worker.import('numpy');

math.factorial(10)                 // 3628800
os.path.split('aaa/bbb')           // ['aaa', 'bbb']


let arr = np.array([4, 9, 16])     // PyObject instance
arr = np.sqrt(arr)                 // still PyObject instance
arr.tolist()                       // [2, 3, 4]

Note that objects proxied by PyObjects don't get garbage collected by Python. You can unreference them manually using unrefAll().

Documentation

#####new Worker([scriptPath], [options]) or, which is the same pytalk.worker(scriptPath, options)

scriptPath
path to the Python script.
options
  • pythonPath - path to the Python binary. Default is python.
  • stdout - callback called when Python script prints something. Default is console.log.
  • stderr - callback called on Python's raised errors. Default is console.log.
  • async - If true, PyObject's methods become async. Default is false. (example)
  • sudo - If true, the python script is ran as root (linux only)

#####Worker.method(methodName) Returns a function(arg1, ..., argN, callback). args are the args passed to the Python method, registered using @pytalk_method(methodName) decorator. callback is a error-first function, called when Python method finishes its work. Use this when you need async version of some sync Python function.

#####Worker.methodSync(methodName) Same thing as Worker.method, except it waits until Python method gets its work done, and returns whatever Python function returns. Uses deasync under the hood.

#####Worker.on(eventName, callback) Registers event handler for eventName. callback gets triggered with (err, args) passed every time pytalk_emit(eventName, args) is called in Python code.

#####Worker.emit(eventName, ...args) Calls Python function, registered with @pytalk_on(eventName) decorator, or through pytalk_on(eventName, callback)

#####Worker.close() Sends exitSignal to Python's event loop. Worker closes as soon as it finishes its current job.

#####Worker.unrefAll() Removes all references to Python objects, proxied by JavaScript objects. This allows Python GC to free resources if it needs to.

#####Worker.import(moduleName) Imports moduleName in Python, and returns a proxy PyObject.

License

MIT

pytalk's People

Contributors

qwazwsx avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.