Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9240867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:15:46+00:00 2026-06-18T08:15:46+00:00

I am trying to do some communication between my ruby process and Python process;

  • 0

I am trying to do some communication between my ruby process and Python process; and I want to use UNIX socket.

Objective:
ruby process “fork and exec” the Python process. In ruby process, create a UNIX socket pair, and pass it to Python.

Ruby code (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 code (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')

command line:

ruby p.rb

Result:

OSError: [Errno 9] Bad file descriptor

I was hoping that the ruby process will pass the file descriptor to the python process, so that these two could send data using these socket.

So, my question:

1) Is it possible to pass open file descriptor between ruby and python process as above?

2) If we can pass around file descriptor between two processes, then what’s wrong in my code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:15:47+00:00Added an answer on June 18, 2026 at 8:15 am

    You were close, but Ruby spawn closes any file descriptors > 2 by default, unless you pass :close_others => false as argument. See the documentation:

    http://apidock.com/ruby/Kernel/spawn

    Working example:

    require 'socket'
    
    r_socket, p_socket = Socket.pair(:UNIX, :DGRAM, 0)
    
    pid = Process.spawn('python', 'p.py', p_socket.fileno.to_s,
                        { :close_others => false })
    
    # Close the python end (we're not using it on the Ruby side)
    p_socket.close
    
    # Wait for some data
    puts r_socket.gets
    
    # Wait for finish
    Process.waitpid(pid)
    

    Python:

    import sys
    import socket
    
    p_fd     = int(sys.argv[1])
    p_socket = socket.fromfd(p_fd, socket.AF_UNIX, socket.SOCK_DGRAM)
    
    p_socket.send("Hello world\n")
    

    Test:

    > ruby p.rb
    Hello world
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to evaluate some technologies for implementing a communication process between some
Trying to create an app that does some socket communication (Writing only). I can
I'm trying to make some communication for my PIC devices and I use UDP
I'm trying to properly connect three processes in order to allow inter-process communication between
I'm trying to get some communication happening between a C# .NET app and some
I'm trying to make some sort of communication between two servers(can be called license
Recently I was trying some practice programs in python and I came across this
I was trying some problems with my 2D ruby array and my LOC reduces
I am trying to implement a P2P communication between two Android phones (one of
I'm trying to assamble a healty communication between two Xbee modules. I've succedd what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.