mirror of
https://github.com/Mayuri-Chan/pyrofork.git
synced 2026-01-07 15:34:52 +00:00
Handle socket.connect() blocking-ness
This commit is contained in:
parent
6f7ec0de03
commit
6736602424
1 changed files with 6 additions and 1 deletions
|
|
@ -21,6 +21,7 @@ import ipaddress
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import socks
|
import socks
|
||||||
|
|
@ -78,7 +79,11 @@ class TCP:
|
||||||
self.socket.settimeout(TCP.TIMEOUT)
|
self.socket.settimeout(TCP.TIMEOUT)
|
||||||
|
|
||||||
async def connect(self, address: tuple):
|
async def connect(self, address: tuple):
|
||||||
self.socket.connect(address)
|
# The socket used by the whole logic is blocking and thus it blocks when connecting.
|
||||||
|
# Offload the task to a thread executor to avoid blocking the main event loop.
|
||||||
|
with ThreadPoolExecutor(1) as executor:
|
||||||
|
await self.loop.run_in_executor(executor, self.socket.connect, address)
|
||||||
|
|
||||||
self.reader, self.writer = await asyncio.open_connection(sock=self.socket)
|
self.reader, self.writer = await asyncio.open_connection(sock=self.socket)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue