最近一个做一个项目,需要多个进程交互。同时,这几个进程需要各自独立启动,并无父子关系,评估后决定使用命名管道方式进行进程通信。为保证消息完整性以及并发安全,使用文件锁保证读写的原子性。代码如下:
import multiprocessing
import os
import traceback
import time
import json
import sys
import fcntl
class Pipe:
def __init__(self, pipe_name):
file_path = "/tmp/"
try:
self.fifo_path = "{}{}.pipe".format(file_path, pipe_name)
os.mkfifo(self.fifo_path)
except Exception:
print(traceback.format_exc())
self.block_file = "/tmp/block_{}.file".format(pipe_name)
if not os.path.exists(self.block_file):
f = open(self.block_file, "w")
f.close()
self.fp_block = open(self.block_file, "w")
def __lock(self, flag=fcntl.LOCK_EX | fcntl.LOCK_NB):
fcntl.flock(self.fp_block.fileno(), flag)
def __unlock(self):
fcntl.flock(self.fp_block.filen