Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Commit b75ab0f

Browse files
authored
Merge pull request #10 from Borkason/master
Various suggestions and code improvements, fixed one possible mistake
2 parents f1d205c + 0588423 commit b75ab0f

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

asgi_ipc.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from __future__ import unicode_literals
2+
23
import mmap
34
import msgpack
4-
import os
5-
import pkg_resources
6-
import posix_ipc
75
import random
86
import six
97
import string
@@ -12,6 +10,8 @@
1210
import time
1311
from asgiref.base_layer import BaseChannelLayer
1412

13+
import pkg_resources
14+
import posix_ipc
1515

1616
__version__ = pkg_resources.require('asgi_ipc')[0].version
1717
MB = 1024 * 1024
@@ -31,7 +31,9 @@ class IPCChannelLayer(BaseChannelLayer):
3131
little... heavier than that.
3232
"""
3333

34-
def __init__(self, prefix="asgi", expiry=60, group_expiry=86400, capacity=10, channel_capacity=None, channel_memory=100*MB, group_memory=20*MB):
34+
def __init__(self, prefix="asgi", expiry=60, group_expiry=86400,
35+
capacity=10, channel_capacity=None,
36+
channel_memory=100 * MB, group_memory=20 * MB):
3537
super(IPCChannelLayer, self).__init__(
3638
expiry=expiry,
3739
group_expiry=group_expiry,
@@ -40,16 +42,24 @@ def __init__(self, prefix="asgi", expiry=60, group_expiry=86400, capacity=10, ch
4042
)
4143
self.thread_lock = threading.Lock()
4244
self.prefix = prefix
43-
self.channel_store = MemoryDict("/%s-chan" % self.prefix, size=channel_memory)
45+
self.channel_store = MemoryDict(
46+
"/%s-chan" % self.prefix,
47+
size=channel_memory
48+
)
4449
# Set containing all groups to flush
45-
self.group_store = MemoryDict("/%s-group" % self.prefix, size=group_memory)
50+
self.group_store = MemoryDict(
51+
"/%s-group" % self.prefix,
52+
size=group_memory
53+
)
4654

47-
### ASGI API ###
55+
# --------
56+
# ASGI API
57+
# --------
4858

4959
extensions = ["flush", "groups"]
5060

5161
def send(self, channel, message):
52-
# Typecheck
62+
# Type check
5363
assert isinstance(message, dict), "message is not a dict"
5464
assert self.valid_channel_name(channel), "channel name not valid"
5565
# Write message into the correct message queue
@@ -61,17 +71,20 @@ def send(self, channel, message):
6171
channel_list.append([message, time.time() + self.expiry])
6272
self.channel_store[channel] = channel_list
6373

64-
def receive(self, channels, block=False):
74+
def receive(self, channels):
6575
if not channels:
6676
return None, None
6777
channels = list(channels)
68-
assert all(self.valid_channel_name(channel) for channel in channels), "one or more channel names invalid"
78+
assert all(
79+
self.valid_channel_name(channel) for channel in channels
80+
), "one or more channel names invalid"
6981
random.shuffle(channels)
7082
# Try to pop off all of the named channels
7183
with self.thread_lock:
7284
for channel in channels:
7385
channel_list = self.channel_store.get(channel, [])
74-
# Keep looping on the channel until we hit no messages or an unexpired one
86+
# Keep looping on the channel until
87+
# we hit no messages or an unexpired one
7588
while True:
7689
try:
7790
# Popleft equivalent
@@ -83,16 +96,16 @@ def receive(self, channels, block=False):
8396
return channel, message
8497
except IndexError:
8598
break
86-
# If the channel is now empty, delete its key
87-
if not channel_list and channel in self.channel_store:
88-
del self.channel_store[channel]
99+
# If the channel is now empty, delete its key
100+
if not channel_list and channel in self.channel_store:
101+
del self.channel_store[channel]
89102
return None, None
90103

91104
def new_channel(self, pattern):
92105
assert isinstance(pattern, six.text_type)
93106
# Keep making channel names till one isn't present.
94107
while True:
95-
random_string = "".join(random.choice(string.ascii_letters) for i in range(12))
108+
random_string = "".join(random.sample(string.ascii_letters, 12))
96109
assert pattern.endswith("!") or pattern.endswith("?")
97110
new_name = pattern + random_string
98111
# To see if it's present we open the queue without O_CREAT
@@ -102,7 +115,9 @@ def new_channel(self, pattern):
102115
else:
103116
continue
104117

105-
### Groups extension ###
118+
# ----------------
119+
# Groups extension
120+
# ----------------
106121

107122
def group_add(self, group, channel):
108123
"""
@@ -154,7 +169,9 @@ def send_group(self, group, message):
154169
except self.ChannelFull:
155170
pass
156171

157-
### Flush extension ###
172+
# ---------------
173+
# Flush extension
174+
# ---------------
158175

159176
def flush(self):
160177
"""
@@ -165,7 +182,7 @@ def flush(self):
165182
self.group_store.flush()
166183

167184
def __str__(self):
168-
return "%s(hosts=%s)" % (self.__class__.__name__, self.hosts)
185+
return "%s(prefix=%s)" % (self.__class__.__name__, self.prefix)
169186

170187

171188
class MemoryDatastructure(object):
@@ -209,7 +226,8 @@ def __init__(self, path, size=None):
209226
)
210227
except ValueError as e:
211228
raise ValueError(
212-
"Unable to allocate shared memory segment (potentially out of memory).\n" +
229+
"Unable to allocate shared memory segment"
230+
"(potentially out of memory).\n"
213231
"Error was: %s" % e
214232
)
215233
self.mmap = mmap.mmap(self.shm.fd, self.size)

0 commit comments

Comments
 (0)