[Misc] Upgrade more code to Python 3.10 (#26463)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
@@ -18,6 +18,7 @@ On the client side, run:
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import contextlib
|
||||
import gc
|
||||
import importlib.util
|
||||
import json
|
||||
@@ -605,17 +606,13 @@ async def benchmark(
|
||||
|
||||
pbar = None if disable_tqdm else tqdm(total=len(input_requests))
|
||||
|
||||
# This can be used once the minimum Python version is 3.10 or higher,
|
||||
# and it will simplify the code in limited_request_func.
|
||||
# semaphore = (asyncio.Semaphore(max_concurrency)
|
||||
# if max_concurrency else contextlib.nullcontext())
|
||||
semaphore = asyncio.Semaphore(max_concurrency) if max_concurrency else None
|
||||
semaphore = (
|
||||
asyncio.Semaphore(max_concurrency)
|
||||
if max_concurrency
|
||||
else contextlib.nullcontext()
|
||||
)
|
||||
|
||||
async def limited_request_func(request_func_input, session, pbar):
|
||||
if semaphore is None:
|
||||
return await request_func(
|
||||
request_func_input=request_func_input, session=session, pbar=pbar
|
||||
)
|
||||
async with semaphore:
|
||||
return await request_func(
|
||||
request_func_input=request_func_input, session=session, pbar=pbar
|
||||
|
||||
@@ -469,11 +469,6 @@ def make_async(
|
||||
return _async_wrapper
|
||||
|
||||
|
||||
def _next_task(iterator: AsyncGenerator[T, None], loop: AbstractEventLoop) -> Task:
|
||||
# Can use anext() in python >= 3.10
|
||||
return loop.create_task(iterator.__anext__()) # type: ignore[arg-type]
|
||||
|
||||
|
||||
async def merge_async_iterators(
|
||||
*iterators: AsyncGenerator[T, None],
|
||||
) -> AsyncGenerator[tuple[int, T], None]:
|
||||
@@ -491,7 +486,7 @@ async def merge_async_iterators(
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
awaits = {_next_task(pair[1], loop): pair for pair in enumerate(iterators)}
|
||||
awaits = {loop.create_task(anext(it)): (i, it) for i, it in enumerate(iterators)}
|
||||
try:
|
||||
while awaits:
|
||||
done, _ = await asyncio.wait(awaits.keys(), return_when=FIRST_COMPLETED)
|
||||
@@ -500,7 +495,7 @@ async def merge_async_iterators(
|
||||
try:
|
||||
item = await d
|
||||
i, it = pair
|
||||
awaits[_next_task(it, loop)] = pair
|
||||
awaits[loop.create_task(anext(it))] = pair
|
||||
yield i, item
|
||||
except StopAsyncIteration:
|
||||
pass
|
||||
|
||||
@@ -290,9 +290,7 @@ class MsgpackDecoder:
|
||||
_log_insecure_serialization_warning()
|
||||
|
||||
def decode(self, bufs: Union[bytestr, Sequence[bytestr]]) -> Any:
|
||||
if isinstance(bufs, (bytes, bytearray, memoryview, zmq.Frame)):
|
||||
# TODO - This check can become `isinstance(bufs, bytestr)`
|
||||
# as of Python 3.10.
|
||||
if isinstance(bufs, bytestr): # type: ignore
|
||||
return self.decoder.decode(bufs)
|
||||
|
||||
self.aux_buffers = bufs
|
||||
|
||||
Reference in New Issue
Block a user