Skip to content

Instantly share code, notes, and snippets.

@ajdavis
Created February 5, 2019 03:33
Show Gist options
  • Save ajdavis/a1b1377a7623332312bb30c2ec057e24 to your computer and use it in GitHub Desktop.
Save ajdavis/a1b1377a7623332312bb30c2ec057e24 to your computer and use it in GitHub Desktop.
class AsyncSocketWrapper:
# Sockets are wrapped in this simplified class and passed down
# to the pymongo <=> libmongocrypt integration layer.
def __init__(self, sockinfo):
self._sockinfo = sockinfo
def send(self, buffer):
# libmongocrypt calls this function on one of its worker threads.
event = threading.Event()
loop = asyncio.get_event_loop()
def _async_send():
# On the main thread: do I/O then unblock worker thread
await loop.sock_sendall(self._sockinfo, buffer)
event.set()
loop.call_soon(_async_send)
event.wait()
def recv(self, to_recv):
# libmongocrypt calls this function on one of its worker threads.
event = threading.Event()
loop = asyncio.get_event_loop()
buffer = ""
def _async_recv():
# On the main thread: do I/O then unblock worker thread
buffer = await loop.sock_recv(self._sockinfo, to_recv)
event.set()
loop.call_soon(_async_recv)
event.wait()
return buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment