20
20
import os
21
21
import threading
22
22
import time
23
- from typing import Any , ParamSpec , TypeVar , cast
23
+ from typing import Any , ParamSpec , Self , TypeVar , cast
24
24
25
25
from prettyqt import core
26
26
31
31
T = TypeVar ("T" )
32
32
33
33
34
- def chunked_iter (src : Iterable , size : int ):
35
- if not src :
36
- return
37
- src_iter = iter (src )
38
- while cur_chunk := list (itertools .islice (src_iter , size )):
39
- yield cur_chunk
40
-
41
-
42
34
class AsyncRunner :
43
35
"""A runner which runs long-lasting functions using a thread pool."""
44
36
@@ -59,7 +51,7 @@ def __init__(self, max_threads: int | None = None):
59
51
self ._signaller = _FutureDoneSignaller ()
60
52
self ._signaller .future_done_signal .connect (self ._resume_coroutine )
61
53
62
- def __enter__ (self : T ) -> T :
54
+ def __enter__ (self ) -> Self :
63
55
return self
64
56
65
57
def __exit__ (self , * exc_info : object ):
@@ -108,7 +100,7 @@ def close(self):
108
100
109
101
async def run (
110
102
self , func : Callable [Params , T ], * args : Params .args , ** kwargs : Params .kwargs
111
- ) -> T :
103
+ ) -> T | None :
112
104
"""Run the given function in a thread.
113
105
114
106
While it is running, yields
@@ -149,7 +141,7 @@ async def run_parallel( # type:ignore[override]
149
141
# that try to submit functions to execute in threads would only be resumed
150
142
# much later, causing a noticeable slow down in the application.
151
143
batch_size = max (self ._max_threads // 2 , 1 )
152
- for function_batch in chunked_iter (funcs , batch_size ):
144
+ for function_batch in itertools . batched (funcs , batch_size ):
153
145
# Submit all functions from the current batch to the thread pool,
154
146
# using the _AsyncTask to track the futures and await when they finish.
155
147
task = _AsyncTask ({self ._pool .submit (f ) for f in function_batch })
0 commit comments