-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.py
More file actions
214 lines (174 loc) · 8.13 KB
/
Copy paththreadpool.py
File metadata and controls
214 lines (174 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
===========================================================
THREAD POOL
===========================================================
Description:
Python'da verimli paralel görev yürütümü için profesyonel thread pool (iş parçacığı havuzu)
implementasyonu. Bu modül, çoklu iş parçacığı ile görevlerin aynı anda çalıştırılmasını
sağlar ve thread-safe bir şekilde görev yönetimi sunar.
Author:
Mefamex (info@mefamex.com) (https://mefamex.com)
Features:
- Verimli paralel görev yürütümü
- Dinamik thread havuzu yönetimi
- Thread-safe görev ekleme ve sonuç toplama
- Esnek sonuç yönetimi (saklama veya anında yazdırma)
- Performans ölçümü ve süre hesaplama
- Basit ve sezgisel API
Modules:
- threading: Thread yönetimi ve senkronizasyon
- queue: Thread-safe görev kuyruğu
- time: Performans ölçümü ve zamanlama
- random: Test amaçlı rastgele sayı üretimi
Classes:
- POOL: Ana thread pool sınıfı, görev yönetimi ve paralel çalıştırma için
Functions:
- __init__(max_threads, logFuture, ResultwhenDone): Thread pool'u başlatır
- submit(func, *args, **kwargs): Yeni görev ekler
- join(): Tüm görevlerin tamamlanmasını bekler ve sonuçları döner
- _worker(): Thread worker fonksiyonu (dahili)
Usage:
1. POOL nesnesi oluşturun (max_threads, logFuture, ResultwhenDone parametreleri ile)
2. submit() metodu ile görevleri ekleyin
3. join() metodu ile tüm görevlerin tamamlanmasını bekleyin
4. Sonuçları, geçen süreyi ve toplam işlem süresini alın
Requirements:
- Python 3.6 veya üstü
- Dependencies:
- threading (built-in)
- queue (built-in)
- time (built-in)
- random (built-in)
Installation:
1. Tek dosya olarak kullanım:
- Bu dosyayı projenize kopyalayın
- Python -> from threadpool import POOL
Documentation:
- Detaylı kullanım örnekleri: `README.md`
- API dokümantasyonu: Kod içi docstring'ler
License:
MIT Lisansı (https://opensource.org/licenses/MIT)
Changelog:
- 1.0.0 (2024-09-08): İlk sürüm
- 1.1.0 (2025-07-20): Dokümantasyon iyileştirmeleri ve usage example güncellenmesi
Contributors: None
Contact:
- Email: info@mefamex.com
- Website: https://mefamex.com
- GitHub: https://github.com/Mefamex/Python_Code_Snippets
Additional Information:
Bu modül, I/O yoğun işlemler için uygundur. CPU yoğun işlemler için multiprocessing
modülü tercih edilebilir. Thread havuzu, bellek kullanımını optimize ederken
aynı anda çalışan görev sayısını kontrol eder.
Notes:
- Bu modül I/O yoğun işlemler için optimize edilmiştir
- Çok fazla thread kullanmak sistem performansını olumsuz etkileyebilir
- Thread-safe tasarım sayesinde güvenli görev yönetimi sağlar
- Performans metrikleri otomatik olarak hesaplanır
Disclaimer and Legal Notice:
Bu yazılım, herhangi bir garanti olmaksızın "olduğu gibi" sağlanmaktadır.
Yazar, bu yazılımın kullanımı sonucunda oluşabilecek zararlardan sorumlu değildir.
Kullanıcılar, yazılımı kendi sorumlulukları altında kullanmalıdır.
MIT Lisansı kapsamında açık kaynak olarak dağıtılır ve kullanıcılar lisans
koşullarına uymakla yükümlüdür. Yazılımın değiştirilmesi, dağıtılması veya
kullanılması lisans koşullarına uygun olmalıdır.
===========================================================
"""
__version__ = "1.1.0"
__author__ = "Mefamex"
__email__ = "info@mefamex.com"
__license__ = "MIT"
__status__ = "STABLE"
__project_name__ = "thread-pool"
__url__ = "https://mefamex.com"
__url_github__ = "https://github.com/Mefamex/Python_Code_Snippets"
__copyright__ = "Copyright (c) 2025 Mefamex"
__description__ = "Python için verimli thread pool implementasyonu - paralel görev yürütümü"
__date__ = "2024-09-08"
__date_modify__ = "2025-07-20"
__python_version__ = ">=3.6"
__dependencies__ = {
"threading": "built-in",
"queue": "built-in",
"time": "built-in",
"random": "built-in"
}
#===============================================================================
#============================ IMPORTS =========================================
import threading
from queue import Queue
from time import time, sleep
from random import randint
#==============================================================================
#============================ CLASS POOL ======================================
class POOL:
"""
A thread pool for efficient parallel task execution.
Args:
max_threads (int, optional): Maximum number of threads in the pool. Defaults to 10.
logFuture (bool, optional): Whether to store task results in a list. Defaults to True.
ResultwhenDone (bool, optional): Whether to print task results immediately after completion. Defaults to False.
"""
def __init__(self, max_threads:int=10, logFuture:bool=True, ResultwhenDone:bool=False):
self.ResultwhenDone, self.logFuture =ResultwhenDone, logFuture
self.task_queue, self.lock = Queue(), threading.Lock()
self.futures , self.max_threads= [], max_threads
self.start_time,self.task_times = time(), []
def _worker(self):
"""Worker function executed by each thread in the pool."""
while True:
func, args, kwargs = self.task_queue.get()
task_start_time = time()
try:
result = func(*args, **kwargs)
self.task_times.append( time() - task_start_time)
if self.logFuture: self.futures.append(result)
if self.ResultwhenDone: print(result)
finally:self.task_queue.task_done()
def submit(self, func, *args, **kwargs) -> None:
"""Submits a new task for execution.
Args:
func: The function to be executed.
*args: Positional arguments for the function.
**kwargs: Keyword arguments for the function.
"""
if not self.start_time:self.start_time = time()
self.task_queue.put((func, args, kwargs))
with self.lock:
if self.task_queue.qsize() > 0 and threading.active_count() < self.max_threads:
threading.Thread(target=self._worker).start()
def join(self, verbose: bool = True) -> tuple[list, float, float]:
"""Waits for all tasks to complete and returns results.
Returns:
tuple: A tuple containing the list of task results, elapsed time, and total time.
"""
self.task_queue.join()
elapsed_time = time() - self.start_time
total_time = sum( [q for q in self.task_times ])
if verbose: print(f"Toplam geçen süre: {elapsed_time:.2f} saniye\n kurtarılan :{total_time-elapsed_time:.2f}")
return self.futures,elapsed_time,total_time
#===============================================================================
#============================ USAGE EXAMPLE ====================================
"""
POOL modülünün temel kullanımı aşağıdaki gibidir:
from threadpool import POOL
from time import sleep
def kare_al(x):
sleep(1) # Simülasyon için gecikme
return x * x
if __name__ == "__main__":
pool = POOL(max_threads=5, logFuture=True, ResultwhenDone=True)
for i in range(10):
pool.submit(kare_al, i)
sonuc_listesi, gecen_sure, toplam_is_suresi = pool.join()
print("Sonuçlar:", sonuc_listesi)
print(f"Toplam geçen süre: {gecen_sure:.2f} sn, Toplam iş süresi: {toplam_is_suresi:.2f} sn")
# Kısa açıklama:
# - POOL nesnesi oluşturulur.
# - submit ile görevler eklenir.
# - join ile tüm görevlerin bitmesi beklenir ve sonuçlar alınır.
# - logFuture=True ile sonuçlar kaydedilir, ResultwhenDone=True ile her görev bittiğinde ekrana yazılır.
"""
#===============================================================================