From c52cd9cc92335c369ccd146c6f2ca33823a69962 Mon Sep 17 00:00:00 2001 From: AN Long Date: Thu, 4 Jul 2024 23:23:28 +0800 Subject: [PATCH] Using a thread pool to avoid TAsyncSocket.open block the event loop (#287) * Using a thread pool to avoid TAsyncSocket.open block the event loop * Compatible with Python3.6 --- thriftpy2/contrib/aio/socket.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/thriftpy2/contrib/aio/socket.py b/thriftpy2/contrib/aio/socket.py index 8d1e49d..8f4195a 100644 --- a/thriftpy2/contrib/aio/socket.py +++ b/thriftpy2/contrib/aio/socket.py @@ -2,13 +2,17 @@ from __future__ import absolute_import, division -import ssl import asyncio import errno import os import socket +import ssl import struct import sys +if sys.version_info >= (3, 7, 0): + from asyncio import get_running_loop +else: + from asyncio import _get_running_loop as get_running_loop from thriftpy2.transport import TTransportException from thriftpy2.transport._ssl import ( @@ -17,6 +21,7 @@ DEFAULT_CIPHERS ) + MAC_OR_BSD = sys.platform == 'darwin' or sys.platform.startswith('freebsd') @@ -145,7 +150,11 @@ async def open(self): if self.connect_timeout: self.raw_sock.settimeout(self.connect_timeout) - self.raw_sock.connect(addr) + loop = get_running_loop() + # The raw_sock.connect may block the event loop if the target + # server is slow or unreachable. Using a thread pool to solve it + # as a quick and dirty way. See #270. + await loop.run_in_executor(None, lambda: self.raw_sock.connect(addr)) if self.socket_timeout: self.raw_sock.settimeout(self.socket_timeout)