How to implement shared memory in Python (mmap.mmap)

1. Purpose

I want to change variables inside the code by accessing them from the outside while executing Python code.

2 Contents

I want to access and change the variable a from the outside while executing Python code. Consider the code (test1.py) that always stores the contents of the text file "TEST.txt" in the variable a as shown below. Even if the contents of TEST.txt are changed while test1.py is being executed, the value of the variable a in test1.py is not updated. (The value of TEST.txt before the change remains)

54.JPG

3 What to do

Read the contents of TEST.txt into memory. test1.py and test2.py share memory. test1.py is reading the memory value into variable a. test2.py changes the value of memory. Whenever test1.py is executed, the value is read from memory and stored in the variable a. When test2.py is executed, the memory value is changed, and test1.py reads the updated value in test2.py.

55.JPG

56.JPG

4 code

Below is the code that implements the above behavior. Use mmap.mmap-> You can store the contents of the read file in memory.

Please drive test2.py after running test1.py first.

/test1.py



import mmap
with open("example.txt", "r+b") as f:
       #mmap.mmap(fileno, length[, tagname[, access[, offset]]])Map length bytes from the specified file.
       #If length is 0, the maximum length of the map is the current file size.
    mm = mmap.mmap(f.fileno(), 0) #Read the contents of the file and write to mm. mm is the sequence of bits"01010101..." //mmap.mmap(fileno, length[, tagname[, access[, offset]]])
    while True:
        mm.seek(0) #Read memory mm from the head(seek(0)), Store the read value in mm.
        mode=mm.readline() #Write the contents of memory mm to the variable mode.
        print(mode)

/test2.py


import mmap

with open("example.txt", "r+b") as f:
#Read the contents of the file and write to mm. mm is the sequence of bits"01010101..."
    mm = mmap.mmap(f.fileno(), 0)  
#Memory mm b"01"Rewrite to. example example.Since the length of the txt data is 2, specify the length of the data to be written as 2. example example.Data with a length different from the txt data cannot be written.
    mm[:] = b"01"  

/example.txt


#Please write the data in binary. To write to memory
00

Recommended Posts

How to implement shared memory in Python (mmap.mmap)
How to implement Discord Slash Command in Python
How to develop in Python
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to implement Python EXE for Windows in Docker container
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
I tried to implement permutation in Python
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
I tried to implement PLSA in Python 2
How to implement nested serializer in drf-flex-fields
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to implement Scroll View in pythonista 1
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
Implement Enigma in python
How to deal with memory leaks in matplotlib.pyplot
How to use the C library in Python
Try to implement Oni Maitsuji Miserable in python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to install Python
How to generate permutations in Python and C ++
How to implement Rails helper-like functionality in Django
How to embed a variable in a python string
How to use Python Image Library in python3 series
How to create a JSON file in Python
Implement recommendations in Python
How to install python
Implement XENO in python
Summary of how to use MNIST in Python
How to implement a gradient picker in Houdini
How to specify TLS version in python requests
I tried to implement TOPIC MODEL in Python
How to notify a Discord channel in Python
How to get the files in the [Python] folder