mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-30 10:07:14 +02:00
Use native asyncio.to_thread if available
This commit is contained in:
@ -30,6 +30,20 @@ else:
|
|||||||
from asyncio import get_event_loop as aio_get_running_loop
|
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
|
PY2 = sys.version_info[0] == 2 # DO NOT REMOVE IT. ESP8266/ESP32 depend on it
|
||||||
IS_CYGWIN = sys.platform.startswith("cygwin")
|
IS_CYGWIN = sys.platform.startswith("cygwin")
|
||||||
IS_WINDOWS = WINDOWS = sys.platform.startswith("win")
|
IS_WINDOWS = WINDOWS = sys.platform.startswith("win")
|
||||||
@ -37,13 +51,6 @@ IS_MACOS = sys.platform.startswith("darwin")
|
|||||||
MISSING = object()
|
MISSING = object()
|
||||||
string_types = (str,)
|
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):
|
def is_bytes(x):
|
||||||
return isinstance(x, (bytes, memoryview, bytearray))
|
return isinstance(x, (bytes, memoryview, bytearray))
|
||||||
|
@ -19,10 +19,10 @@ import shutil
|
|||||||
from functools import cmp_to_key
|
from functools import cmp_to_key
|
||||||
|
|
||||||
import click
|
import click
|
||||||
from starlette.concurrency import run_in_threadpool
|
|
||||||
|
|
||||||
from platformio import fs
|
from platformio import fs
|
||||||
from platformio.cache import ContentCache
|
from platformio.cache import ContentCache
|
||||||
|
from platformio.compat import aio_to_thread
|
||||||
from platformio.device.list.util import list_logical_devices
|
from platformio.device.list.util import list_logical_devices
|
||||||
from platformio.home.rpc.handlers.base import BaseRPCHandler
|
from platformio.home.rpc.handlers.base import BaseRPCHandler
|
||||||
from platformio.http import HTTPSession, ensure_internet_on
|
from platformio.http import HTTPSession, ensure_internet_on
|
||||||
@ -33,7 +33,7 @@ class HTTPAsyncSession(HTTPSession):
|
|||||||
self, *args, **kwargs
|
self, *args, **kwargs
|
||||||
):
|
):
|
||||||
func = super().request
|
func = super().request
|
||||||
return await run_in_threadpool(func, *args, **kwargs)
|
return await aio_to_thread(func, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class OSRPC(BaseRPCHandler):
|
class OSRPC(BaseRPCHandler):
|
||||||
|
@ -22,13 +22,13 @@ import threading
|
|||||||
|
|
||||||
import click
|
import click
|
||||||
from ajsonrpc.core import JSONRPC20DispatchException
|
from ajsonrpc.core import JSONRPC20DispatchException
|
||||||
from starlette.concurrency import run_in_threadpool
|
|
||||||
|
|
||||||
from platformio import __main__, __version__, app, fs, proc, util
|
from platformio import __main__, __version__, app, fs, proc, util
|
||||||
from platformio.compat import (
|
from platformio.compat import (
|
||||||
IS_WINDOWS,
|
IS_WINDOWS,
|
||||||
aio_create_task,
|
aio_create_task,
|
||||||
aio_get_running_loop,
|
aio_get_running_loop,
|
||||||
|
aio_to_thread,
|
||||||
get_locale_encoding,
|
get_locale_encoding,
|
||||||
is_bytes,
|
is_bytes,
|
||||||
)
|
)
|
||||||
@ -177,7 +177,7 @@ class PIOCoreRPC(BaseRPCHandler):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def _call_subprocess(args, options):
|
async def _call_subprocess(args, options):
|
||||||
result = await run_in_threadpool(
|
result = await aio_to_thread(
|
||||||
proc.exec_command,
|
proc.exec_command,
|
||||||
[get_core_fullpath()] + args,
|
[get_core_fullpath()] + args,
|
||||||
cwd=options.get("cwd") or os.getcwd(),
|
cwd=options.get("cwd") or os.getcwd(),
|
||||||
@ -197,7 +197,7 @@ class PIOCoreRPC(BaseRPCHandler):
|
|||||||
exit_code,
|
exit_code,
|
||||||
)
|
)
|
||||||
|
|
||||||
return await run_in_threadpool(
|
return await aio_to_thread(
|
||||||
_thread_safe_call, args=args, cwd=options.get("cwd") or os.getcwd()
|
_thread_safe_call, args=args, cwd=options.get("cwd") or os.getcwd()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from ajsonrpc.core import JSONRPC20DispatchException
|
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.home.rpc.handlers.base import BaseRPCHandler
|
||||||
from platformio.registry.client import RegistryClient
|
from platformio.registry.client import RegistryClient
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ class RegistryRPC(BaseRPCHandler):
|
|||||||
async def call_client(method, *args, **kwargs):
|
async def call_client(method, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
client = RegistryClient()
|
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
|
except Exception as exc: # pylint: disable=bare-except
|
||||||
raise JSONRPC20DispatchException(
|
raise JSONRPC20DispatchException(
|
||||||
code=5000, message="Registry Call Error", data=str(exc)
|
code=5000, message="Registry Call Error", data=str(exc)
|
||||||
|
Reference in New Issue
Block a user