Created
February 5, 2019 03:33
-
-
Save ajdavis/a1b1377a7623332312bb30c2ec057e24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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