Skip to content

Instantly share code, notes, and snippets.

@D3f0
Created February 27, 2017 03:31
Show Gist options
  • Save D3f0/259947149edf82d16a6c451263591284 to your computer and use it in GitHub Desktop.
Save D3f0/259947149edf82d16a6c451263591284 to your computer and use it in GitHub Desktop.
NodeJS - Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting hello.py\n"
]
}
],
"source": [
"%%writefile hello.py\n",
"import os\n",
"import json\n",
"print \"Python Start\"\n",
"# 0: stdin, 1: stdout, 3: stderr\n",
"to_node = os.fdopen(3, \"w+\")\n",
"\n",
"print to_node.write(json.dumps({'status': 'ok'}))\n",
"print \"Python End\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Spawning proces\n",
"PID: 51055\n",
"Python Start\n",
"None\n",
"Python End\n",
"0 null\n",
"child process exited with code 0\n"
]
}
],
"source": [
"%%bash -c node\n",
"\n",
"const spawn = require('child_process').spawn;\n",
"\n",
"console.log(\"Spawning proces\");\n",
"var child = spawn('python2', ['hello.py'], {\n",
" stdio:['inherit', 'inherit', 'inherit', 'ipc']\n",
"});\n",
"\n",
"console.log(\"PID:\", child.pid)\n",
"\n",
"child.on('message', function(message) {\n",
" console.log('Received message...');\n",
" console.log(message);\n",
"});\n",
"\n",
"child.on('exit', (e1, e2) => {\n",
" console.log(e1, e2); \n",
"});\n",
"\n",
"child.on('error', function(err) {\n",
" console.err(\":(\" + err);\n",
"});\n",
"\n",
"child.on('close', (code) => {\n",
" console.log(`child process exited with code ${code}`);\n",
"});\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ejecutando `ls`"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"stdout: total 16\n",
"lrwxr-xr-x 1 root wheel 8B Oct 31 11:07 X11 -> /opt/X11\n",
"lrwxr-xr-x 1 root wheel 8B Oct 31 11:07 X11R6 -> /opt/X11\n",
"drwxr-xr-x 1060 root wheel 35K Feb 2 12:16 bin\n",
"drwxr-xr-x 261 root wheel 8.7K Dec 14 10:30 include\n",
"drwxr-xr-x 302 root wheel 10K Feb 2 12:16 lib\n",
"drwxr-xr-x 205 root wheel 6.8K Feb 2 12:18 libexec\n",
"drwxr-xr-x 24 nahuel wheel 816B Feb 22 08:48 local\n",
"drwxr-xr-x 246 root wheel 8.2K Feb 2 12:16 sbin\n",
"drwxr-xr-x 47 root wheel 1.6K Oct 9 02:32 share\n",
"drwxr-xr-x 5 root wheel 170B Dec 21 04:13 standalone\n",
"\n",
"child process exited with code 0\n"
]
}
],
"source": [
"%%bash -c node\n",
"const spawn = require('child_process').spawn;\n",
"const ls = spawn('ls', ['-lh', '/usr']);\n",
"\n",
"ls.stdout.on('data', (data) => {\n",
" console.log(`stdout: ${data}`);\n",
"});\n",
"\n",
"ls.stderr.on('data', (data) => {\n",
" console.log(`stderr: ${data}`);\n",
"});\n",
"\n",
"ls.on('close', (code) => {\n",
" console.log(`child process exited with code ${code}`);\n",
"});"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment