PyConnector
pyconnector
: Bridge the gap between Node.JS and Python applications
Both Node.JS and Python have their own unique strengths and weaknesses when it comes to developing applications. This module provides a simple and crude interface to embed and use Python code for number crunching queries.
Installation
Just type npm install pyconnector
and you are ready to go.
Usage
Node.JS
Create a new instance of PyConnector
class having the desired options.
// minimal.jsconst Path = const PyConnector = // create connectorvar PyAPI = // local port or remote IP:Port string '192.168.0.12:9000' endpoint: 24001 // pass null or remove option to prevent spawning a child process path: Path; // query python resolvers { console console console console // kill python process PyAPI}
Python
Install Python dependencies
pip install nodeconnector argparse
Create minimal.py file
# minimal.pyimport sysimport timeimport argparseimport nodeconnector # argument parsing, PyConnector automatically sends thisparser = argparse.ArgumentParser( description = 'Python Exposed API' )parser.add_argument( '--pynodeport', help = 'PyConnector Node.JS query port', default = 24001 )args = parser.parse_args() # create interfacenodeq = nodeconnector.Interface( ) # python version querydef nodeq_version( args, ctx = {} ): return ( '%d.%d.%d' % ( sys.version_info[ 0 ], sys.version_info[ 1 ], sys.version_info[ 2 ] ) ) # increment value querydef nodeq_increment( args, ctx = {} ): # return value ctx[ 'inc' ] += 1 args[ 'value' ] = ctx[ 'inc' ] return args # queries are executed on a separate thread, a context dict can be used to pass datanodeq.handle( 'pyversion', nodeq_version )nodeq.handle( 'increment', nodeq_increment, dict( inc = 0 ) ) # launch APInodeq.listen( port = args.pynodeport ) # waitwhile( True ): time.sleep( 0.001 )
Background
PyConnector is intended to be used for rapid prototyping within POC applications. For production and advanced use cases, its recommended to use this ZeroMQ package, which this module heavily relies upon.
License (MIT)
Copyright (C) 2019 Cristian Dobre