filebus
Social Media Photo by Juan Encalada on Unsplash
A communication channel based on files watcher.
Example
You can clone this repository and inotifywait-spawn in the same folder, enter into the filebus/example
directory and launch npm start
to see Python and NodeJS communicating with each other.
You can also grasp most of this utility via the following example:
const FileBus = ; // new instance with either an input to listen for// or an output to send messages ... or both!const fb = input // optional input file to watch output // optional output file to write; // INPUT relatd API // will be triggered when the file will contain// "ready" with eventually extra data as JSONfb; // triggered when there are reading errors// from the watched input filefb; // stop listening to events// no more events triggeredfb; // OUTPUT related API // will write the following into the output file// "ready {"optional":"data"}"// if there is a watcher listening for "ready",// or any other event name, it will be invoked// receiving the object {optional: 'data'}fb; // HANDSHAKE // whenever the input/output files are between two// different instances of FileBus or two different PLs// the handshake, from one of the two sides, grants// that both instances are setup and ready to react fb = '.python' '.js' true;// Python counterpart: fb = FileBus('.js', '.python') // will happen once Python respondsfb; fb;
Communicate with Python 3
This module contains a python/filebus.py file where you can import FileBus
and use pretty much exact same API.
Please note the python file also relies in this module dependencies, so that inotifywait-spawn module must be installed too, otherwise its python/inotifywait.py
would be missing.
inotifywait
requires the Python 3 inotify_simple module.
sudo pip3 install inotify_simple
To import FileBus
you can write the following:
import osimport sys from filebus import FileBus fb = # wait for events to trigger import time # close and cleanup the file
Practical Applications
The countdown project is fully based on FileBus to drive e-paper displays, via Python 3, from Node.js.