Last active
August 12, 2020 12:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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