2
我想在ruby進程和Python進程之間進行一些通信;我想使用UNIX套接字。如何使用Unix Socket在ruby和Python之間進行通信
目標: ruby進程「fork and exec」Python進程。在ruby進程中,創建一個UNIX套接字對,並將其傳遞給Python。
Ruby代碼(p.rb):
require 'socket'
r_socket, p_socket = Socket.pair(:UNIX, :DGRAM, 0)
# I was hoping this file descriptor would be available in the child process
pid = Process.spawn('python', 'p.py', p_socket.fileno.to_s)
Process.waitpid(pid)
Python代碼(p.py):
import sys
import os
import socket
# get the file descriptor from command line
p_fd = int(sys.argv[1])
socket.fromfd(p_fd, socket.AF_UNIX, socket.SOCK_DGRAM)
# f_socket = os.fdopen(p_fd)
# os.write(p_fd, 'h')
命令行:
ruby p.rb
結果:
OSError: [Errno 9] Bad file descriptor
我希望ruby進程將文件描述符傳遞給python進程,以便這兩個可以使用這些套接字發送數據。
所以,我的問題:
1)是否有可能通過紅寶石和如上Python進程之間打開的文件描述符?
2)如果我們可以在兩個進程之間傳遞文件描述符,那麼我的代碼有什麼問題。