-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshared_memory_record.py
More file actions
65 lines (54 loc) · 1.88 KB
/
shared_memory_record.py
File metadata and controls
65 lines (54 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
import os
from _filelock import FileLock
base_dir = os.path.abspath(os.path.dirname(__file__))
class SharedMemoryRecorder:
cache_names_file = f"{base_dir}/.sm_names"
lock_file = f"{base_dir}/.lock"
lock = FileLock(lock_file)
@classmethod
def load_cache(cls):
if os.path.exists(cls.cache_names_file):
return json.loads(open(cls.cache_names_file, 'rb').read())
return []
@classmethod
def remove_lock_file(cls):
if os.path.exists(cls.lock_file):
os.remove(cls.lock_file)
@classmethod
def release_last_sm(cls):
from shared import SharedMemory
with cls.lock:
if os.path.exists(cls.cache_names_file):
_is_error = False
for name, size, shm_id in cls.load_cache():
try:
sm = SharedMemory(name, False, size)
sm.close()
sm.unlink()
except Exception as e:
print(str(e).split()[-1], end=";")
_is_error = True
if _is_error: print()
os.remove(cls.cache_names_file)
@classmethod
def save_sm_name(cls, name, size, shm_id=None):
with cls.lock:
data = cls.load_cache()
data.append([name, size, shm_id])
open(cls.cache_names_file, 'wb').write(json.dumps(data).encode("utf8"))
def release_last_shm():
return SharedMemoryRecorder.release_last_sm()
def release_sm(sm_name, size, shm_id):
import subprocess
from shared import SharedMemory
from platform import system
try:
sm = SharedMemory(sm_name, create=False, size=size)
sm.close()
sm.unlink()
if system().startswith("Linux"):
subprocess.call(f"ipcrm -m {shm_id}", shell=True)
except:
pass
return True