如何在运行的python代码和nodejs之间进行通信 [英] How can I communicate between running python code and nodejs
问题描述
我想运行一些python代码并与nodejs express服务器通信.到目前为止,我可以使我的nodejs服务器通过两种机制之一调用python函数,以生成python任务或使其与zerorpc python服务器通信.
I'd like to have some python code running and communicating with a nodejs express server. So far, I can get my nodejs server to call python functions via one of two mechanisms, either to spawn a python task or to have it talk to a zerorpc python server.
首先, http://www .sohamkamani.com/blog/2015/08/21/python-nodejs-comm/,这有效:
var express = require( "express" );
var http = require( "http" );
var app = express();
var server = http.createServer( app ).listen( 3000 );
var io = require( "socket.io" )( server );
app.use( express.static( "./public" ) );
io.on( "connection", function( socket ) {
// Repeat interval is in milliseconds
setInterval( function() {
var spawn = require( 'child_process' ).spawn,
py = spawn( 'python', [ 'mytime.py' ] ),
message = '';
py.stdout.on( 'data', function( data ) {
message += data.toString();
});
py.stdout.on( 'end', function() {
socket.emit( "message", message );
});
}, 50 );
});
mytime.py在哪里
Where mytime.py is
from datetime import datetime
import sys
def main():
now = datetime.now()
sys.stdout.write( now.strftime( "%-d %b %Y %H:%M:%S.%f" ) )
并使用zerorpc http://www.zerorpc.io/,如果此python代码正在运行:
And with zerorpc http://www.zerorpc.io/, if this python code is running:
from datetime import datetime
import sys
import zerorpc
class MyTime( object ):
def gettime( self ):
now = datetime.now()
return now.strftime( "%-d %b %Y %H:%M:%S.%f" )
s = zerorpc.Server( MyTime() )
s.bind( "tcp://0.0.0.0:4242" )
s.run()
此nodejs代码有效:
This nodejs code works:
var express = require( "express" );
var http = require( "http" );
var app = express();
var server = http.createServer( app ).listen( 3000 );
var io = require( "socket.io" )( server );
var zerorpc = require( "zerorpc" );
var client = new zerorpc.Client();
client.connect( "tcp://127.0.0.1:4242" );
app.use( express.static( "./public" ) );
io.on( "connection", function( socket ) {
// Repeat interval is in milliseconds
setInterval( function() {
client.invoke( "gettime", function( error, res, more ) {
socket.emit( "message", res.toString( 'utf8' ) );
} );
}, 50 );
});
但是我想做的是,不仅仅是调用python函数,我还想运行一个单独的python进程,并将消息发送到nodejs服务器,该服务器监听并处理它们.我已经尝试过使用中间件socketio-wildcard,但是如果我尝试在与nodejs express服务器相同的端口上设置带有Zerorpc的python服务器,则会给出 zmq.error.ZMQError:地址已在使用错误.
But what I'd like to be able to do is instead of just having python functions called, I'd like a separate python process running and sending messages to the nodejs server which listens for them and then handles them. I've experimented with middleware socketio-wildcard, but if I try to set up a python server with zerorpc on the same port as the nodejs express server, it gives a zmq.error.ZMQError: Address already in use error.
我知道我没有考虑这个权利-我知道由于我的天赋,我在进程间通信方面缺少一些逻辑-所以如果有更好的方法来从python进程发送消息在nodejs服务器监听的情况下,我无所适从.
I know that I'm not thinking about this right--I know that I'm missing some logic around interprocess communication due to my naïveté here--so if there is a better way to do message sending from a python process with a nodejs server listening, I'm all ears.
有什么想法吗?
非常感谢!
推荐答案
For those trying to figure this out, here's a solution thanks to Zeke Alexandre Nierenberg
对于node.js服务器代码:
For the node.js server code:
var express = require( "express" );
var app = express();
var http = require( "http" );
app.use( express.static( "./public" ) ); // where the web page code goes
var http_server = http.createServer( app ).listen( 3000 );
var http_io = require( "socket.io" )( http_server );
http_io.on( "connection", function( httpsocket ) {
httpsocket.on( 'python-message', function( fromPython ) {
httpsocket.broadcast.emit( 'message', fromPython );
});
});
以及向其发送消息的python代码:
and the python code that sends it messages:
from datetime import datetime
from socketIO_client import SocketIO, LoggingNamespace
import sys
while True:
with SocketIO( 'localhost', 3000, LoggingNamespace ) as socketIO:
now = datetime.now()
socketIO.emit( 'python-message', now.strftime( "%-d %b %Y %H:%M:%S.%f" ) )
socketIO.wait( seconds=1 )
Voilà!
这篇关于如何在运行的python代码和nodejs之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!