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 committed Oct 16, 2023
1 parent 1446386 commit 0cdb1d8
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 @@ -3718,7 +3718,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 @@ -3821,7 +3823,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 @@ -4230,7 +4234,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 @@ -4257,7 +4262,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 0cdb1d8

Please sign in to comment.