pyloid 0.26.2__py3-none-any.whl → 0.26.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyloid/thread_pool.py CHANGED
@@ -1,501 +1,648 @@
1
- from PySide6.QtCore import QRunnable, QThreadPool, QDeadlineTimer, QObject, Signal
2
- from typing import Callable, Optional, Union
1
+ from PySide6.QtCore import (
2
+ QRunnable,
3
+ QThreadPool,
4
+ QDeadlineTimer,
5
+ QObject,
6
+ Signal,
7
+ )
8
+ from typing import (
9
+ Callable,
10
+ Optional,
11
+ Union,
12
+ )
13
+
3
14
 
4
15
  class PyloidRunnable(QRunnable):
5
- """
6
- A runnable task class that extends QRunnable from Qt.
7
-
8
- Defines a unit of work that can be executed in a thread pool.
9
- """
10
-
11
- def __init__(self):
12
- """
13
- Initializes a PyloidRunnable instance.
14
-
15
- By default, auto-delete is enabled.
16
- """
17
- super().__init__()
18
- self.setAutoDelete(True)
19
-
20
- def get_auto_delete(self) -> bool:
21
- """
22
- Returns whether the task is automatically deleted after completion in the thread pool.
23
-
24
- The default value is True.
25
-
26
- Returns
27
- -------
28
- bool
29
- True if the task is automatically deleted after completion, False if manual deletion is required
30
-
31
- Examples
32
- --------
33
- ```python
34
- from pyloid.thread_pool import PyloidRunnable
35
- import time
36
-
37
- class Worker(PyloidRunnable):
38
- def run(self):
39
- time.sleep(1)
40
- print("Task executed")
41
-
42
- worker = Worker()
43
- print(worker.get_auto_delete())
44
- ```
45
- """
46
- return self.autoDelete()
47
-
48
- def set_auto_delete(self, value: bool) -> None:
49
- """
50
- Sets whether the task is automatically deleted after completion in the thread pool.
51
-
52
- Parameters
53
- ----------
54
- value : bool
55
- True to enable auto-delete after task completion,
56
- False to require manual deletion
57
-
58
- Examples
59
- --------
60
- ```python
61
- from pyloid.thread_pool import PyloidRunnable
62
- import time
63
-
64
- class Worker(PyloidRunnable):
65
- def run(self):
66
- time.sleep(1)
67
- print("Task executed")
68
-
69
- worker = Worker()
70
- worker.set_auto_delete(False)
71
- ```
72
- """
73
- self.setAutoDelete(value)
74
-
75
- def run(self) -> None:
76
- """
77
- Defines the actual work to be executed in the thread pool.
78
-
79
- This method must be implemented in subclasses.
80
-
81
- Examples
82
- --------
83
- ```python
84
- from pyloid.thread_pool import PyloidRunnable, PyloidThreadPool
85
- import time
86
-
87
- class Worker(PyloidRunnable):
88
- def run(self):
89
- time.sleep(1)
90
- print("Task executed")
91
-
92
- worker = Worker()
93
- thread_pool = PyloidThreadPool()
94
- thread_pool.start(worker)
95
- ```
96
- """
97
- pass
98
-
16
+ """
17
+ A runnable task class that extends QRunnable from Qt.
18
+
19
+ Defines a unit of work that can be executed in a thread pool.
20
+ """
21
+
22
+ def __init__(
23
+ self,
24
+ ):
25
+ """
26
+ Initializes a PyloidRunnable instance.
27
+
28
+ By default, auto-delete is enabled.
29
+ """
30
+ super().__init__()
31
+ self.setAutoDelete(True)
32
+
33
+ def get_auto_delete(
34
+ self,
35
+ ) -> bool:
36
+ """
37
+ Returns whether the task is automatically deleted after completion in the thread pool.
38
+
39
+ The default value is True.
40
+
41
+ Returns
42
+ -------
43
+ bool
44
+ True if the task is automatically deleted after completion, False if manual deletion is required
45
+
46
+ Examples
47
+ --------
48
+ ```python
49
+ from pyloid.thread_pool import (
50
+ PyloidRunnable,
51
+ )
52
+ import time
53
+
54
+
55
+ class Worker(PyloidRunnable):
56
+ def run(
57
+ self,
58
+ ):
59
+ time.sleep(1)
60
+ print('Task executed')
61
+
62
+
63
+ worker = Worker()
64
+ print(worker.get_auto_delete())
65
+ ```
66
+ """
67
+ return self.autoDelete()
68
+
69
+ def set_auto_delete(
70
+ self,
71
+ value: bool,
72
+ ) -> None:
73
+ """
74
+ Sets whether the task is automatically deleted after completion in the thread pool.
75
+
76
+ Parameters
77
+ ----------
78
+ value : bool
79
+ True to enable auto-delete after task completion,
80
+ False to require manual deletion
81
+
82
+ Examples
83
+ --------
84
+ ```python
85
+ from pyloid.thread_pool import (
86
+ PyloidRunnable,
87
+ )
88
+ import time
89
+
90
+
91
+ class Worker(PyloidRunnable):
92
+ def run(
93
+ self,
94
+ ):
95
+ time.sleep(1)
96
+ print('Task executed')
97
+
98
+
99
+ worker = Worker()
100
+ worker.set_auto_delete(False)
101
+ ```
102
+ """
103
+ self.setAutoDelete(value)
104
+
105
+ def run(
106
+ self,
107
+ ) -> None:
108
+ """
109
+ Defines the actual work to be executed in the thread pool.
110
+
111
+ This method must be implemented in subclasses.
112
+
113
+ Examples
114
+ --------
115
+ ```python
116
+ from pyloid.thread_pool import (
117
+ PyloidRunnable,
118
+ PyloidThreadPool,
119
+ )
120
+ import time
121
+
122
+
123
+ class Worker(PyloidRunnable):
124
+ def run(
125
+ self,
126
+ ):
127
+ time.sleep(1)
128
+ print('Task executed')
129
+
130
+
131
+ worker = Worker()
132
+ thread_pool = PyloidThreadPool()
133
+ thread_pool.start(worker)
134
+ ```
135
+ """
136
+ pass
137
+
138
+
99
139
  class PyloidDefaultSignals(QObject):
100
- """
101
- Default signal class.
102
-
103
- Defines signals used for task start, completion, and error occurrence in the thread pool.
104
-
105
- Attributes
106
- ----------
107
- started : Signal
108
- Signal emitted when a task starts
109
- finished : Signal
110
- Signal emitted when a task completes
111
- error : Signal(str)
112
- Signal emitted when an error occurs
113
- progress : Signal(int)
114
- Signal emitted when progress changes
115
-
116
- Examples
117
- --------
118
- ```python
119
- from pyloid.thread_pool import PyloidRunnable, PyloidThreadPool, PyloidDefaultSignals
120
- import time
121
-
122
- class Worker(PyloidRunnable):
123
- def __init__(self):
124
- super().__init__()
125
- self.signals = PyloidDefaultSignals()
126
-
127
- def run(self):
128
- for i in range(101):
129
- self.signals.progress.emit(i)
130
- time.sleep(0.1)
131
-
132
- worker = Worker()
133
-
134
- worker.signals.finished.connect(lambda: print("Task completed."))
135
- worker.signals.error.connect(lambda error: print(f"Error occurred: {error}"))
136
- worker.signals.progress.connect(lambda progress: print(f"Progress: {progress}%"))
137
-
138
- thread_pool = PyloidThreadPool()
139
- thread_pool.start(worker)
140
- ```
141
- """
142
- started = Signal()
143
- finished = Signal()
144
- error = Signal(str)
145
- progress = Signal(int)
146
-
140
+ """
141
+ Default signal class.
142
+
143
+ Defines signals used for task start, completion, and error occurrence in the thread pool.
144
+
145
+ Attributes
146
+ ----------
147
+ started : Signal
148
+ Signal emitted when a task starts
149
+ finished : Signal
150
+ Signal emitted when a task completes
151
+ error : Signal(str)
152
+ Signal emitted when an error occurs
153
+ progress : Signal(int)
154
+ Signal emitted when progress changes
155
+
156
+ Examples
157
+ --------
158
+ ```python
159
+ from pyloid.thread_pool import (
160
+ PyloidRunnable,
161
+ PyloidThreadPool,
162
+ PyloidDefaultSignals,
163
+ )
164
+ import time
165
+
166
+
167
+ class Worker(PyloidRunnable):
168
+ def __init__(
169
+ self,
170
+ ):
171
+ super().__init__()
172
+ self.signals = PyloidDefaultSignals()
173
+
174
+ def run(
175
+ self,
176
+ ):
177
+ for i in range(101):
178
+ self.signals.progress.emit(i)
179
+ time.sleep(0.1)
180
+
181
+
182
+ worker = Worker()
183
+
184
+ worker.signals.finished.connect(lambda: print('Task completed.'))
185
+ worker.signals.error.connect(lambda error: print(f'Error occurred: {error}'))
186
+ worker.signals.progress.connect(lambda progress: print(f'Progress: {progress}%'))
187
+
188
+ thread_pool = PyloidThreadPool()
189
+ thread_pool.start(worker)
190
+ ```
191
+ """
192
+
193
+ started = Signal()
194
+ finished = Signal()
195
+ error = Signal(str)
196
+ progress = Signal(int)
197
+
147
198
 
148
199
  class PyloidThreadPool:
149
- def __init__(self):
150
- self.thread_pool = QThreadPool.globalInstance()
151
-
152
- def start(self, runnable: Union[PyloidRunnable, Callable], priority: int = ...) -> None:
153
- """
154
- Adds a task to the thread pool and executes it.
155
-
156
- Parameters
157
- ----------
158
- runnable : Union[PyloidRunnable, Callable]
159
- Task to be executed
160
- priority : int
161
- Task priority
162
-
163
- Examples
164
- --------
165
- ```python
166
- from pyloid.thread_pool import PyloidRunnable, PyloidThreadPool
167
- import time
168
-
169
- class Worker(PyloidRunnable):
170
- def run(self):
171
- time.sleep(1)
172
- print("Task executed")
173
-
174
- worker = Worker()
175
- thread_pool = PyloidThreadPool()
176
- thread_pool.start(worker)
177
- ```
178
- """
179
- self.thread_pool.start(runnable, priority)
180
-
181
- def active_thread_count(self) -> int:
182
- """
183
- Returns the number of currently active threads.
184
-
185
- Returns
186
- -------
187
- int
188
- Number of currently active threads
189
-
190
- Examples
191
- --------
192
- ```python
193
- from pyloid.thread_pool import PyloidThreadPool
194
-
195
- thread_pool = PyloidThreadPool()
196
- print(thread_pool.active_thread_count())
197
- ```
198
- """
199
- return self.thread_pool.activeThreadCount()
200
-
201
- def max_thread_count(self) -> int:
202
- """
203
- Returns the maximum number of threads that can run simultaneously in the thread pool.
204
-
205
- Returns
206
- -------
207
- int
208
- Maximum number of threads
209
-
210
- Examples
211
- --------
212
- ```python
213
- from pyloid.thread_pool import PyloidThreadPool
214
-
215
- thread_pool = PyloidThreadPool()
216
- print(thread_pool.max_thread_count())
217
- ```
218
- """
219
- return self.thread_pool.maxThreadCount()
220
-
221
- def set_max_thread_count(self, max_thread_count: int) -> None:
222
- """
223
- Sets the maximum number of threads that can run simultaneously in the thread pool.
224
-
225
- Parameters
226
- ----------
227
- max_thread_count : int
228
- Maximum number of threads
229
-
230
- Examples
231
- --------
232
- ```python
233
- from pyloid.thread_pool import PyloidThreadPool
234
-
235
- thread_pool = PyloidThreadPool()
236
- thread_pool.set_max_thread_count(10)
237
- ```
238
- """
239
- self.thread_pool.setMaxThreadCount(max_thread_count)
240
-
241
- def reserve_thread(self) -> None:
242
- """
243
- Reserves a thread in the thread pool.
244
-
245
- Examples
246
- --------
247
- ```python
248
- from pyloid.thread_pool import PyloidThreadPool, PyloidRunnable
249
- import time
250
-
251
- class Worker(PyloidRunnable):
252
- def run(self):
253
- time.sleep(1)
254
- print("Task executed on reserved thread")
255
-
256
- # Create thread pool
257
- thread_pool = PyloidThreadPool()
258
-
259
- # Reserve thread
260
- thread_pool.reserve_thread()
261
-
262
- try:
263
- # Execute task on reserved thread
264
- worker = Worker()
265
- thread_pool.start_on_reserved_thread(worker)
266
-
267
- # Wait for task completion
268
- thread_pool.wait_for_done()
269
- finally:
270
- # Important: Reserved threads must be released
271
- thread_pool.release_thread()
272
- ```
273
- """
274
- self.thread_pool.reserveThread()
275
-
276
- def release_thread(self) -> None:
277
- """
278
- Releases a reserved thread in the thread pool.
279
-
280
- Examples
281
- --------
282
- ```python
283
- from pyloid.thread_pool import PyloidThreadPool
284
-
285
- thread_pool = PyloidThreadPool()
286
- thread_pool.release_thread()
287
- ```
288
- """
289
- self.thread_pool.releaseThread()
290
-
291
- def clear(self) -> None:
292
- """
293
- Removes all pending tasks from the thread pool.
294
-
295
- Examples
296
- --------
297
- ```python
298
- from pyloid.thread_pool import PyloidThreadPool
299
-
300
- thread_pool = PyloidThreadPool()
301
- thread_pool.clear()
302
- ```
303
- """
304
- self.thread_pool.clear()
305
-
306
- # def contains(self, thread: QThread) -> bool:
307
- # return self.thread_pool.contains(thread)
308
-
309
- def get_expiry_timeout(self) -> int:
310
- """
311
- Returns the thread expiry timeout in the thread pool.
312
-
313
- Returns
314
- -------
315
- int
316
- Thread expiry timeout
317
-
318
- Examples
319
- --------
320
- ```python
321
- from pyloid.thread_pool import PyloidThreadPool
322
-
323
- thread_pool = PyloidThreadPool()
324
- print(thread_pool.get_expiry_timeout())
325
- ```
326
- """
327
- return self.thread_pool.expiryTimeout()
328
-
329
- def set_expiry_timeout(self, expiry_timeout: int) -> None:
330
- """
331
- Sets the thread expiry timeout in the thread pool.
332
-
333
- Parameters
334
- ----------
335
- expiry_timeout : int
336
- Thread expiry timeout
337
-
338
- Examples
339
- --------
340
- ```python
341
- from pyloid.thread_pool import PyloidThreadPool
342
-
343
- thread_pool = PyloidThreadPool()
344
- thread_pool.set_expiry_timeout(1000)
345
- ```
346
- """
347
- self.thread_pool.setExpiryTimeout(expiry_timeout)
348
-
349
- # def set_stack_size(self, stack_size: int) -> None:
350
- # self.thread_pool.setStackSize(stack_size)
351
-
352
- # def stack_size(self) -> int:
353
- # return self.thread_pool.stackSize()
354
-
355
- # def set_thread_priority(self, priority: QThread.Priority) -> None:
356
- # self.thread_pool.setThreadPriority(priority)
357
-
358
- # def thread_priority(self) -> QThread.Priority:
359
- # return self.thread_pool.threadPriority()
360
-
361
- def start_on_reserved_thread(self, runnable: QRunnable) -> None:
362
- """
363
- Executes a task on a reserved thread.
364
-
365
- Parameters
366
- ----------
367
- runnable : QRunnable
368
- Task to be executed
369
-
370
- Examples
371
- --------
372
- ```python
373
- from pyloid.thread_pool import PyloidThreadPool, PyloidRunnable
374
- import time
375
-
376
- class Worker(PyloidRunnable):
377
- def run(self):
378
- time.sleep(1)
379
- print("Task executed on reserved thread")
380
-
381
- worker = Worker()
382
- thread_pool = PyloidThreadPool()
383
- thread_pool.reserve_thread()
384
- thread_pool.start_on_reserved_thread(worker)
385
- thread_pool.wait_for_done()
386
- thread_pool.release_thread()
387
- ```
388
- """
389
- self.thread_pool.startOnReservedThread(runnable)
390
-
391
- def try_start(self, runnable: Union[QRunnable, Callable]) -> bool:
392
- """
393
- Adds a new task to the thread pool and attempts to execute it immediately.
394
-
395
- Only executes the task if the thread pool has available capacity.
396
- Operates in a non-blocking manner and does not start the task if the thread pool is full.
397
-
398
- Parameters
399
- ----------
400
- runnable : Union[QRunnable, Callable]
401
- Task to be executed
402
-
403
- Returns
404
- -------
405
- bool
406
- True: Task successfully started
407
- False: Task could not be started because the thread pool is full
408
-
409
- Examples
410
- --------
411
- ```python
412
- from pyloid.thread_pool import PyloidThreadPool, PyloidRunnable
413
-
414
- class Worker(PyloidRunnable):
415
- def run(self):
416
- print("Task executed")
417
-
418
- thread_pool = PyloidThreadPool()
419
- worker = Worker()
420
-
421
- if thread_pool.try_start(worker):
422
- print("Task started")
423
- else:
424
- print("Task could not be started because the thread pool is full")
425
- ```
426
- """
427
- return self.thread_pool.tryStart(runnable)
428
-
429
- def try_take(self, runnable: QRunnable) -> bool:
430
- """
431
- Attempts to remove a specific task from the thread pool queue.
432
-
433
- Only removes tasks that have not yet been executed. Tasks that are already running cannot be removed.
434
- Used when task cancellation is needed.
435
-
436
- Parameters
437
- ----------
438
- runnable : QRunnable
439
- Task to be removed from the queue
440
-
441
- Returns
442
- -------
443
- bool
444
- True: Task successfully removed from the queue
445
- False: Task could not be removed (already running or not found)
446
-
447
- Examples
448
- --------
449
- ```python
450
- from pyloid.thread_pool import PyloidThreadPool, PyloidRunnable
451
-
452
- class Worker(PyloidRunnable):
453
- def run(self):
454
- print("Task executed")
455
-
456
- thread_pool = PyloidThreadPool()
457
- worker = Worker()
458
-
459
- # Add task
460
- thread_pool.start(worker)
461
-
462
- # Attempt to remove task
463
- if thread_pool.try_take(worker):
464
- print("Task removed from queue")
465
- else:
466
- print("Task could not be removed (already running or not found)")
467
- ```
468
- """
469
- return self.thread_pool.tryTake(runnable)
470
-
471
- def wait_for_done(self, timeout: Optional[int] = None) -> bool:
472
- """
473
- Waits for tasks to complete.
474
-
475
- If no timeout is specified, waits indefinitely until completion.
476
-
477
- Parameters
478
- ----------
479
- timeout : Optional[int]
480
- Wait time
481
-
482
- Returns
483
- -------
484
- bool
485
- True if tasks completed, False otherwise
486
-
487
- Examples
488
- --------
489
- ```python
490
- from pyloid.thread_pool import PyloidThreadPool
491
-
492
- thread_pool = PyloidThreadPool()
493
- thread_pool.wait_for_done()
494
-
495
- print("Tasks completed.")
496
- ```
497
- """
498
- if timeout is None:
499
- return self.thread_pool.waitForDone(-1)
500
- else:
501
- return self.thread_pool.waitForDone(timeout)
200
+ def __init__(
201
+ self,
202
+ ):
203
+ self.thread_pool = QThreadPool.globalInstance()
204
+
205
+ def start(
206
+ self,
207
+ runnable: Union[
208
+ PyloidRunnable,
209
+ Callable,
210
+ ],
211
+ priority: int = ...,
212
+ ) -> None:
213
+ """
214
+ Adds a task to the thread pool and executes it.
215
+
216
+ Parameters
217
+ ----------
218
+ runnable : Union[PyloidRunnable, Callable]
219
+ Task to be executed
220
+ priority : int
221
+ Task priority
222
+
223
+ Examples
224
+ --------
225
+ ```python
226
+ from pyloid.thread_pool import (
227
+ PyloidRunnable,
228
+ PyloidThreadPool,
229
+ )
230
+ import time
231
+
232
+
233
+ class Worker(PyloidRunnable):
234
+ def run(
235
+ self,
236
+ ):
237
+ time.sleep(1)
238
+ print('Task executed')
239
+
240
+
241
+ worker = Worker()
242
+ thread_pool = PyloidThreadPool()
243
+ thread_pool.start(worker)
244
+ ```
245
+ """
246
+ self.thread_pool.start(
247
+ runnable,
248
+ priority,
249
+ )
250
+
251
+ def active_thread_count(
252
+ self,
253
+ ) -> int:
254
+ """
255
+ Returns the number of currently active threads.
256
+
257
+ Returns
258
+ -------
259
+ int
260
+ Number of currently active threads
261
+
262
+ Examples
263
+ --------
264
+ ```python
265
+ from pyloid.thread_pool import (
266
+ PyloidThreadPool,
267
+ )
268
+
269
+ thread_pool = PyloidThreadPool()
270
+ print(thread_pool.active_thread_count())
271
+ ```
272
+ """
273
+ return self.thread_pool.activeThreadCount()
274
+
275
+ def max_thread_count(
276
+ self,
277
+ ) -> int:
278
+ """
279
+ Returns the maximum number of threads that can run simultaneously in the thread pool.
280
+
281
+ Returns
282
+ -------
283
+ int
284
+ Maximum number of threads
285
+
286
+ Examples
287
+ --------
288
+ ```python
289
+ from pyloid.thread_pool import (
290
+ PyloidThreadPool,
291
+ )
292
+
293
+ thread_pool = PyloidThreadPool()
294
+ print(thread_pool.max_thread_count())
295
+ ```
296
+ """
297
+ return self.thread_pool.maxThreadCount()
298
+
299
+ def set_max_thread_count(
300
+ self,
301
+ max_thread_count: int,
302
+ ) -> None:
303
+ """
304
+ Sets the maximum number of threads that can run simultaneously in the thread pool.
305
+
306
+ Parameters
307
+ ----------
308
+ max_thread_count : int
309
+ Maximum number of threads
310
+
311
+ Examples
312
+ --------
313
+ ```python
314
+ from pyloid.thread_pool import (
315
+ PyloidThreadPool,
316
+ )
317
+
318
+ thread_pool = PyloidThreadPool()
319
+ thread_pool.set_max_thread_count(10)
320
+ ```
321
+ """
322
+ self.thread_pool.setMaxThreadCount(max_thread_count)
323
+
324
+ def reserve_thread(
325
+ self,
326
+ ) -> None:
327
+ """
328
+ Reserves a thread in the thread pool.
329
+
330
+ Examples
331
+ --------
332
+ ```python
333
+ from pyloid.thread_pool import (
334
+ PyloidThreadPool,
335
+ PyloidRunnable,
336
+ )
337
+ import time
338
+
339
+
340
+ class Worker(PyloidRunnable):
341
+ def run(
342
+ self,
343
+ ):
344
+ time.sleep(1)
345
+ print('Task executed on reserved thread')
346
+
347
+
348
+ # Create thread pool
349
+ thread_pool = PyloidThreadPool()
350
+
351
+ # Reserve thread
352
+ thread_pool.reserve_thread()
353
+
354
+ try:
355
+ # Execute task on reserved thread
356
+ worker = Worker()
357
+ thread_pool.start_on_reserved_thread(worker)
358
+
359
+ # Wait for task completion
360
+ thread_pool.wait_for_done()
361
+ finally:
362
+ # Important: Reserved threads must be released
363
+ thread_pool.release_thread()
364
+ ```
365
+ """
366
+ self.thread_pool.reserveThread()
367
+
368
+ def release_thread(
369
+ self,
370
+ ) -> None:
371
+ """
372
+ Releases a reserved thread in the thread pool.
373
+
374
+ Examples
375
+ --------
376
+ ```python
377
+ from pyloid.thread_pool import (
378
+ PyloidThreadPool,
379
+ )
380
+
381
+ thread_pool = PyloidThreadPool()
382
+ thread_pool.release_thread()
383
+ ```
384
+ """
385
+ self.thread_pool.releaseThread()
386
+
387
+ def clear(
388
+ self,
389
+ ) -> None:
390
+ """
391
+ Removes all pending tasks from the thread pool.
392
+
393
+ Examples
394
+ --------
395
+ ```python
396
+ from pyloid.thread_pool import (
397
+ PyloidThreadPool,
398
+ )
399
+
400
+ thread_pool = PyloidThreadPool()
401
+ thread_pool.clear()
402
+ ```
403
+ """
404
+ self.thread_pool.clear()
405
+
406
+ # def contains(self, thread: QThread) -> bool:
407
+ # return self.thread_pool.contains(thread)
408
+
409
+ def get_expiry_timeout(
410
+ self,
411
+ ) -> int:
412
+ """
413
+ Returns the thread expiry timeout in the thread pool.
414
+
415
+ Returns
416
+ -------
417
+ int
418
+ Thread expiry timeout
419
+
420
+ Examples
421
+ --------
422
+ ```python
423
+ from pyloid.thread_pool import (
424
+ PyloidThreadPool,
425
+ )
426
+
427
+ thread_pool = PyloidThreadPool()
428
+ print(thread_pool.get_expiry_timeout())
429
+ ```
430
+ """
431
+ return self.thread_pool.expiryTimeout()
432
+
433
+ def set_expiry_timeout(
434
+ self,
435
+ expiry_timeout: int,
436
+ ) -> None:
437
+ """
438
+ Sets the thread expiry timeout in the thread pool.
439
+
440
+ Parameters
441
+ ----------
442
+ expiry_timeout : int
443
+ Thread expiry timeout
444
+
445
+ Examples
446
+ --------
447
+ ```python
448
+ from pyloid.thread_pool import (
449
+ PyloidThreadPool,
450
+ )
451
+
452
+ thread_pool = PyloidThreadPool()
453
+ thread_pool.set_expiry_timeout(1000)
454
+ ```
455
+ """
456
+ self.thread_pool.setExpiryTimeout(expiry_timeout)
457
+
458
+ # def set_stack_size(self, stack_size: int) -> None:
459
+ # self.thread_pool.setStackSize(stack_size)
460
+
461
+ # def stack_size(self) -> int:
462
+ # return self.thread_pool.stackSize()
463
+
464
+ # def set_thread_priority(self, priority: QThread.Priority) -> None:
465
+ # self.thread_pool.setThreadPriority(priority)
466
+
467
+ # def thread_priority(self) -> QThread.Priority:
468
+ # return self.thread_pool.threadPriority()
469
+
470
+ def start_on_reserved_thread(
471
+ self,
472
+ runnable: QRunnable,
473
+ ) -> None:
474
+ """
475
+ Executes a task on a reserved thread.
476
+
477
+ Parameters
478
+ ----------
479
+ runnable : QRunnable
480
+ Task to be executed
481
+
482
+ Examples
483
+ --------
484
+ ```python
485
+ from pyloid.thread_pool import (
486
+ PyloidThreadPool,
487
+ PyloidRunnable,
488
+ )
489
+ import time
490
+
491
+
492
+ class Worker(PyloidRunnable):
493
+ def run(
494
+ self,
495
+ ):
496
+ time.sleep(1)
497
+ print('Task executed on reserved thread')
498
+
499
+
500
+ worker = Worker()
501
+ thread_pool = PyloidThreadPool()
502
+ thread_pool.reserve_thread()
503
+ thread_pool.start_on_reserved_thread(worker)
504
+ thread_pool.wait_for_done()
505
+ thread_pool.release_thread()
506
+ ```
507
+ """
508
+ self.thread_pool.startOnReservedThread(runnable)
509
+
510
+ def try_start(
511
+ self,
512
+ runnable: Union[
513
+ QRunnable,
514
+ Callable,
515
+ ],
516
+ ) -> bool:
517
+ """
518
+ Adds a new task to the thread pool and attempts to execute it immediately.
519
+
520
+ Only executes the task if the thread pool has available capacity.
521
+ Operates in a non-blocking manner and does not start the task if the thread pool is full.
522
+
523
+ Parameters
524
+ ----------
525
+ runnable : Union[QRunnable, Callable]
526
+ Task to be executed
527
+
528
+ Returns
529
+ -------
530
+ bool
531
+ True: Task successfully started
532
+ False: Task could not be started because the thread pool is full
533
+
534
+ Examples
535
+ --------
536
+ ```python
537
+ from pyloid.thread_pool import (
538
+ PyloidThreadPool,
539
+ PyloidRunnable,
540
+ )
541
+
542
+
543
+ class Worker(PyloidRunnable):
544
+ def run(
545
+ self,
546
+ ):
547
+ print('Task executed')
548
+
549
+
550
+ thread_pool = PyloidThreadPool()
551
+ worker = Worker()
552
+
553
+ if thread_pool.try_start(worker):
554
+ print('Task started')
555
+ else:
556
+ print('Task could not be started because the thread pool is full')
557
+ ```
558
+ """
559
+ return self.thread_pool.tryStart(runnable)
560
+
561
+ def try_take(
562
+ self,
563
+ runnable: QRunnable,
564
+ ) -> bool:
565
+ """
566
+ Attempts to remove a specific task from the thread pool queue.
567
+
568
+ Only removes tasks that have not yet been executed. Tasks that are already running cannot be removed.
569
+ Used when task cancellation is needed.
570
+
571
+ Parameters
572
+ ----------
573
+ runnable : QRunnable
574
+ Task to be removed from the queue
575
+
576
+ Returns
577
+ -------
578
+ bool
579
+ True: Task successfully removed from the queue
580
+ False: Task could not be removed (already running or not found)
581
+
582
+ Examples
583
+ --------
584
+ ```python
585
+ from pyloid.thread_pool import (
586
+ PyloidThreadPool,
587
+ PyloidRunnable,
588
+ )
589
+
590
+
591
+ class Worker(PyloidRunnable):
592
+ def run(
593
+ self,
594
+ ):
595
+ print('Task executed')
596
+
597
+
598
+ thread_pool = PyloidThreadPool()
599
+ worker = Worker()
600
+
601
+ # Add task
602
+ thread_pool.start(worker)
603
+
604
+ # Attempt to remove task
605
+ if thread_pool.try_take(worker):
606
+ print('Task removed from queue')
607
+ else:
608
+ print('Task could not be removed (already running or not found)')
609
+ ```
610
+ """
611
+ return self.thread_pool.tryTake(runnable)
612
+
613
+ def wait_for_done(
614
+ self,
615
+ timeout: Optional[int] = None,
616
+ ) -> bool:
617
+ """
618
+ Waits for tasks to complete.
619
+
620
+ If no timeout is specified, waits indefinitely until completion.
621
+
622
+ Parameters
623
+ ----------
624
+ timeout : Optional[int]
625
+ Wait time
626
+
627
+ Returns
628
+ -------
629
+ bool
630
+ True if tasks completed, False otherwise
631
+
632
+ Examples
633
+ --------
634
+ ```python
635
+ from pyloid.thread_pool import (
636
+ PyloidThreadPool,
637
+ )
638
+
639
+ thread_pool = PyloidThreadPool()
640
+ thread_pool.wait_for_done()
641
+
642
+ print('Tasks completed.')
643
+ ```
644
+ """
645
+ if timeout is None:
646
+ return self.thread_pool.waitForDone(-1)
647
+ else:
648
+ return self.thread_pool.waitForDone(timeout)