Interprocess communication between Ruby and Python (POSIX message queue)

Overview

When I was thinking about passing data between ruby and python daemon using redis (Pub / Sub) [^ Streams], I heard a heavenly voice saying that it would be easier to use interprocess communication (POSIX IPC), so I tried it. I tried it.

In addition, since it runs on the raspberry pi this time, we are considering a strategy to reduce the number of processes as much as possible due to power supply problems.

[^ Streams]: From redis5, there is an additional feature called Streams that is more reliable than the Pub / Sub feature.

What is a POSIX message queue?

One of the interprocess communication available through system calls / library functions in POSIX. Asynchronous communication protocol, like a super-simplified version of messaging middleware such as RabbitMQ, without fear of misunderstanding. Since it is POSIX compliant, it can be easily used with a simple API.

Please refer to the following for details.

https://linuxjm.osdn.jp/html/LDP_man-pages/man7/mq_overview.7.html

Whole system

Ruby side implementation (sender side)

It doesn't matter which one you send / receive, but this time I tried Ruby as the sender. I used posix-mqueue as the library to use. Very simple.

require 'posix/mqueue'
require 'json'

def main
  m = POSIX::Mqueue.new("/whatever", msgsize: 8192, maxmsg: 10)
  10.times do |i|
    m.send({ messsage: "hello python world #{i}" }.to_json)
    sleep 1
  end
  m.send ({ message: 'end'}.to_json)
ensure
  m.unlink if m
end

if __FILE__ == $PROGRAM_NAME
  main
end

Python side implementation (receiver side)

This is also horribly simple. The library used is posix_ipc

# -*- coding: utf-8 -*-
import time
import posix_ipc
import json

def main () :
    mq = posix_ipc.MessageQueue("/whatever", posix_ipc.O_CREAT)
    while True:
        data = mq.receive()
        j = json.loads(data[0]) # ->  (message, priority)Come back with tuples
        print j
        if j.get('message') == "end":
            break

if __name__ == "__main__" : 
    main()

result

It was output on the Python side as expected!

$ python main.py 
{u'messsage': u'hello python world 0'}
{u'messsage': u'hello python world 1'}
{u'messsage': u'hello python world 2'}
{u'messsage': u'hello python world 3'}
{u'messsage': u'hello python world 4'}
{u'messsage': u'hello python world 5'}
{u'messsage': u'hello python world 6'}
{u'messsage': u'hello python world 7'}
{u'messsage': u'hello python world 8'}
{u'messsage': u'hello python world 9'}
{u'message': u'end'}

It's a simple source, but I've posted it on github for the time being. https://github.com/y-amadatsu/posix-mq-with-raspberry-pi-os

Digression

By the way, if you use POSIX messages, the message Queue will be displayed under / dev / mqueue. You can also mount it and operate it with ls or rm. This area is very UNIX! It feels like: relaxed:

cat /dev/mqueue/whatever 
QSIZE:350        NOTIFY:0     SIGNO:0     NOTIFY_PID:0     

Recommended Posts

Interprocess communication between Ruby and Python (POSIX message queue)
Differences between Ruby and Python in scope
Differences between Ruby and Python (basic syntax)
Ruby, Python and map
[Ruby vs Python] Benchmark comparison between Rails and Flask
Control other programs from Python (communication between Python and exe)
Difference between Ruby and Python in terms of variables
Python on Ruby and angry Ruby on Python
Python and ruby slice memo
Ruby and Python syntax ~ branch ~
Stack and Queue in Python
Difference between java and python (memo)
Difference between list () and [] in Python
Difference between == and is in python
Scraping with Node, Ruby and Python
Handle posix message queues in python
Cooperation between python module and API
Difference in how to write if statement between ruby ​​and python
Differences between Python, stftime and strptime
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Communicate between Elixir and Python with gRPC
Socket communication and multi-thread processing by Python
difference between statements (statements) and expressions (expressions) in Python
Implementation module "deque" in queue and Python
Eating and comparing programming languages: Python and Ruby
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[Basic grammar] Differences between Ruby / Python / PHP
[Python] Difference between class method and static method
[Python3] Switch between Shift_JIS, UTF-8 and ASCII
[Python Iroha] Difference between List and Tuple
Encrypt with Ruby (Rails) and decrypt with Python
[python] Difference between rand and randn output
Differences in multithreading between Python and Jython
Easy web scraping with Python and Ruby
Correspondence between Python built-in functions and Rust
Exchange encrypted data between Python and C #
Differences in string processing between Python, Ruby, JS, PHP (combination and variable expansion)
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
Difference in writing method to read external source code between Ruby and Python
Correspondence summary of array operation of ruby and python
Instant method grammar for Python and Ruby (studying)
Summary of the differences between PHP and Python
The answer of "1/2" is different between python2 and 3
Specifying the range of ruby and python arrays
[Python] Conversion memo between time data and numerical data
About the difference between "==" and "is" in python
Python hand play (interoperability between CSV and PostgreSQL)
Serial communication between Raspberry pi --Arduino Uno (Python)
About shallow and deep copies of Python / Ruby
Comparison of Python and Ruby (Environment / Grammar / Literal)
The timing when the value of the default argument is evaluated is different between Ruby and Python.