Skip to content

Instantly share code, notes, and snippets.

@mauicv
Last active August 12, 2020 12:17
Show Gist options
  • Save mauicv/ff12aa91b6ab63fb8d8d7bb8d52b003f to your computer and use it in GitHub Desktop.
Save mauicv/ff12aa91b6ab63fb8d8d7bb8d52b003f to your computer and use it in GitHub Desktop.
[Python Subprocess Manager] Class for spawning nodejs subprocess and passing cmd, arg pairs to it and parsing the responses #py-to-js #interface
from subprocess import Popen, PIPE
import json
class Manager:
def __init__(self, name):
self.ps = Popen(
['nodejs', 'src/js/index.js'],
stdin=PIPE, stdout=PIPE)
self.name = name
self.make(name)
def _call(self, cmd, args):
cmd_dict = {
'cmd': cmd,
'args': args
}
cmd_dict_str = json.dumps(cmd_dict)
data = cmd_dict_str.encode()
self.ps.stdin.write(data)
self.ps.stdin.flush()
out = self.ps.stdout.readline()
return json.loads(out)
def make(self, name):
return self._call('make', name)
def reset(self):
return self._call('reset', None)
def some_other_command(self, args):
return self._call('some_other_command', args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment