Skip to content

Commit

Permalink
Unlock the GIL for all capnp functions that do IO
Browse files Browse the repository at this point in the history
IO might block, and its rude to block while holding the GIL, since that
prevents other threads from running.
  • Loading branch information
mlaiosa authored and haata committed Oct 16, 2023
1 parent a30fd77 commit 09f7cd0
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions capnp/lib/capnp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3739,7 +3739,9 @@ cdef class _StreamFdMessageReader(_MessageReader):
cdef schema_cpp.ReaderOptions opts = make_reader_opts(traversal_limit_in_words, nesting_limit)

self._parent = file
self.thisptr = new schema_cpp.StreamFdMessageReader(file.fileno(), opts)
cdef int fd = file.fileno()
with nogil:
self.thisptr = new schema_cpp.StreamFdMessageReader(fd, opts)

def __dealloc__(self):
del self.thisptr
Expand Down Expand Up @@ -3842,7 +3844,9 @@ cdef class _PackedFdMessageReader(_MessageReader):
cdef schema_cpp.ReaderOptions opts = make_reader_opts(traversal_limit_in_words, nesting_limit)

self._parent = file
self.thisptr = new schema_cpp.PackedFdMessageReader(file.fileno(), opts)
cdef int fd = file.fileno()
with nogil:
self.thisptr = new schema_cpp.PackedFdMessageReader(fd, opts)

def __dealloc__(self):
del self.thisptr
Expand Down Expand Up @@ -4251,7 +4255,8 @@ def _write_message_to_fd(int fd, _MessageBuilder message):
:rtype: void
"""
schema_cpp.writeMessageToFd(fd, deref(message.thisptr))
with nogil:
schema_cpp.writeMessageToFd(fd, deref(message.thisptr))


def _write_packed_message_to_fd(int fd, _MessageBuilder message):
Expand All @@ -4278,7 +4283,8 @@ def _write_packed_message_to_fd(int fd, _MessageBuilder message):
:rtype: void
"""
schema_cpp.writePackedMessageToFd(fd, deref(message.thisptr))
with nogil:
schema_cpp.writePackedMessageToFd(fd, deref(message.thisptr))


_global_schema_parser = None
Expand Down

0 comments on commit 09f7cd0

Please sign in to comment.