Skip to content

Commit ad9020c

Browse files
authored
Add 'with nogil' to SubSocket for GIL-Free C++ operations (#643)
add 'with nogil' to SubSocket for GIL-Free C++ operations
1 parent 095f1e2 commit ad9020c

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

msgq/ipc.pxd

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ cdef extern from "msgq/ipc.h":
4848

4949
cdef cppclass SubSocket:
5050
@staticmethod
51-
SubSocket * create()
52-
int connect(Context *, string, string, bool)
53-
Message * receive(bool)
54-
void setTimeout(int)
51+
SubSocket * create() nogil
52+
int connect(Context *, string, string, bool) nogil
53+
Message * receive(bool) nogil
54+
void setTimeout(int) nogil
5555

5656
cdef cppclass PubSocket:
5757
@staticmethod

msgq/ipc_pyx.pyx

+17-8
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,30 @@ cdef class SubSocket:
166166
cdef bool is_owner
167167

168168
def __cinit__(self):
169-
self.socket = cppSubSocket.create()
170-
self.is_owner = True
169+
with nogil:
170+
self.socket = cppSubSocket.create()
171171

172+
self.is_owner = True
172173
if self.socket == NULL:
173174
raise IpcError
174175

175176
def __dealloc__(self):
176177
if self.is_owner:
177-
del self.socket
178+
with nogil:
179+
del self.socket
178180

179181
cdef setPtr(self, cppSubSocket * ptr):
180182
if self.is_owner:
181-
del self.socket
183+
with nogil:
184+
del self.socket
182185

183186
self.is_owner = False
184187
self.socket = ptr
185188

186189
def connect(self, Context context, string endpoint, string address=b"127.0.0.1", bool conflate=False):
187-
r = self.socket.connect(context.context, endpoint, address, conflate)
190+
cdef int r
191+
with nogil:
192+
r = self.socket.connect(context.context, endpoint, address, conflate)
188193

189194
if r != 0:
190195
if errno.errno == errno.EADDRINUSE:
@@ -193,17 +198,21 @@ cdef class SubSocket:
193198
raise IpcError(endpoint)
194199

195200
def setTimeout(self, int timeout):
196-
self.socket.setTimeout(timeout)
201+
with nogil:
202+
self.socket.setTimeout(timeout)
197203

198204
def receive(self, bool non_blocking=False):
199-
msg = self.socket.receive(non_blocking)
205+
cdef cppMessage *msg
206+
with nogil:
207+
msg = self.socket.receive(non_blocking)
200208

201209
if msg == NULL:
202210
return None
203211
else:
204212
sz = msg.getSize()
205213
m = msg.getData()[:sz]
206-
del msg
214+
with nogil:
215+
del msg
207216

208217
return m
209218

0 commit comments

Comments
 (0)