forked from a-yun/distributed-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
30 lines (24 loc) · 828 Bytes
/
utils.py
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
import os
import pickle
def pipeName(s, r):
return './pipes/{0}-{1}'.format(s, r)
class Pipes:
def __init__(self):
self.pipes = dict()
def createPipe(self, sender, receiver, write, blocking=True):
flags = os.O_RDWR
mode = 'wb' if write else 'rb'
if not blocking:
flags = flags | os.O_NONBLOCK
name = pipeName(sender, receiver)
fd = os.open(name, flags)
self.pipes[name] = os.fdopen(fd, mode, buffering=0)
def sendMessage(self, sender, receiver, msg):
pipe = self.pipes[pipeName(sender, receiver)]
pickle.dump(msg, pipe)
def receiveMessage(self, sender, receiver):
pipe = self.pipes[pipeName(sender, receiver)]
try:
return pickle.load(pipe)
except TypeError:
return None