Use native asyncio.to_thread if available

This commit is contained in:
Ivan Kravets
2023-06-24 19:40:54 +03:00
parent f720cd841c
commit 4dc44868ea
4 changed files with 21 additions and 14 deletions

View File

@ -30,6 +30,20 @@ else:
from asyncio import get_event_loop as aio_get_running_loop
if sys.version_info >= (3, 8):
from shlex import join as shlex_join
else:
def shlex_join(split_command):
return " ".join(shlex.quote(arg) for arg in split_command)
if sys.version_info >= (3, 9):
from asyncio import to_thread as aio_to_thread
else:
from starlette.concurrency import run_in_threadpool as aio_to_thread
PY2 = sys.version_info[0] == 2 # DO NOT REMOVE IT. ESP8266/ESP32 depend on it
IS_CYGWIN = sys.platform.startswith("cygwin")
IS_WINDOWS = WINDOWS = sys.platform.startswith("win")
@ -37,13 +51,6 @@ IS_MACOS = sys.platform.startswith("darwin")
MISSING = object()
string_types = (str,)
try:
from shlex import join as shlex_join
except ImportError:
def shlex_join(split_command):
return " ".join(shlex.quote(arg) for arg in split_command)
def is_bytes(x):
return isinstance(x, (bytes, memoryview, bytearray))

View File

@ -19,10 +19,10 @@ import shutil
from functools import cmp_to_key
import click
from starlette.concurrency import run_in_threadpool
from platformio import fs
from platformio.cache import ContentCache
from platformio.compat import aio_to_thread
from platformio.device.list.util import list_logical_devices
from platformio.home.rpc.handlers.base import BaseRPCHandler
from platformio.http import HTTPSession, ensure_internet_on
@ -33,7 +33,7 @@ class HTTPAsyncSession(HTTPSession):
self, *args, **kwargs
):
func = super().request
return await run_in_threadpool(func, *args, **kwargs)
return await aio_to_thread(func, *args, **kwargs)
class OSRPC(BaseRPCHandler):

View File

@ -22,13 +22,13 @@ import threading
import click
from ajsonrpc.core import JSONRPC20DispatchException
from starlette.concurrency import run_in_threadpool
from platformio import __main__, __version__, app, fs, proc, util
from platformio.compat import (
IS_WINDOWS,
aio_create_task,
aio_get_running_loop,
aio_to_thread,
get_locale_encoding,
is_bytes,
)
@ -177,7 +177,7 @@ class PIOCoreRPC(BaseRPCHandler):
@staticmethod
async def _call_subprocess(args, options):
result = await run_in_threadpool(
result = await aio_to_thread(
proc.exec_command,
[get_core_fullpath()] + args,
cwd=options.get("cwd") or os.getcwd(),
@ -197,7 +197,7 @@ class PIOCoreRPC(BaseRPCHandler):
exit_code,
)
return await run_in_threadpool(
return await aio_to_thread(
_thread_safe_call, args=args, cwd=options.get("cwd") or os.getcwd()
)

View File

@ -13,8 +13,8 @@
# limitations under the License.
from ajsonrpc.core import JSONRPC20DispatchException
from starlette.concurrency import run_in_threadpool
from platformio.compat import aio_to_thread
from platformio.home.rpc.handlers.base import BaseRPCHandler
from platformio.registry.client import RegistryClient
@ -24,7 +24,7 @@ class RegistryRPC(BaseRPCHandler):
async def call_client(method, *args, **kwargs):
try:
client = RegistryClient()
return await run_in_threadpool(getattr(client, method), *args, **kwargs)
return await aio_to_thread(getattr(client, method), *args, **kwargs)
except Exception as exc: # pylint: disable=bare-except
raise JSONRPC20DispatchException(
code=5000, message="Registry Call Error", data=str(exc)