npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pytalk

v1.0.5

Published

Bidirectional communication between Node.js and Python

Downloads

15

Readme

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.

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)

#####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