[LINUX] Handle posix message queues in python

Overview

procedure

$ sudo pip install posix_ipc

Implementation


#!/usr/local/env python
# -*- coding: utf-8 -*-

import json
import posix_ipc
import time

if __name__ == "__main__":
    posix_ipc.unlink_message_queue("/queue_name")

    mq = posix_ipc.MessageQueue("/queue_name", posix_ipc.O_CREX)
    counter = 0
    while True:
        counter += 1

        #Enqueue with send, str,Send unicode
        mq.send(json.dumps({"loop_count": counter}))
        print "queue: %d" % counter
        time.sleep(1.0)

    posix_ipc.unlink_message_queue("/queue_name")

#!/usr/local/env python
# -*- coding: utf-8 -*-

import json
import posix_ipc
import time

if __name__ == "__main__":
    mq = posix_ipc.MessageQueue("/queue_name")
    while True:
        msg = mq.receive()
        print json.loads(msg[0])
        time.sleep(0.5)

Recommended Posts

Handle posix message queues in python
Exception message in Python
Handle markdown in python
Handle Parquet in Python
Handle Ambient data in Python
Handle environment variables in Python
Handle complex numbers in Python
Handle NetCDF format data in Python
Handle GDS II format in Python
How to handle Japanese in Python
Handle multiple python versions in one jupyter
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Use cryptography module to handle OpenSSL in Python
Handle zip files with Japanese filenames in Python 3
How to handle datetime type in python sqlite3
Sorted list in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python