@@ -66,40 +66,15 @@ def __init__(self):
66
66
67
67
self .logger .debug ("processor initialization..." )
68
68
69
- self .queue = asyncio .Queue (maxsize = settings .PROXY_QUEUE_SIZE )
70
69
self .proxies_semaphore = asyncio .BoundedSemaphore (settings .NUMBER_OF_CONCURRENT_TASKS )
71
70
self .good_proxies_are_processed = False
72
71
73
72
async def worker (self ):
74
73
await asyncio .gather (* [
75
- self .producer (),
76
- self .consumer (),
74
+ self .process_proxies (),
75
+ self .process_collectors (),
77
76
])
78
77
79
- async def consumer (self ):
80
- while True :
81
- await asyncio .sleep (0.00001 )
82
-
83
- try :
84
- if not self .proxies_semaphore .locked ():
85
- asyncio .ensure_future (self .process_proxy (
86
- * (await self .queue .get ())
87
- ))
88
- except KeyboardInterrupt :
89
- raise
90
- except BaseException as ex :
91
- self .logger .exception (ex )
92
- if settings .DEBUG :
93
- raise ex
94
- await asyncio .sleep (settings .SLEEP_AFTER_ERROR_PERIOD )
95
-
96
- async def producer (self ):
97
- while True :
98
- await asyncio .gather (* [
99
- self .process_proxies (),
100
- self .process_collectors (),
101
- ])
102
-
103
78
async def process_proxies (self ):
104
79
while True :
105
80
await asyncio .sleep (0.00001 )
@@ -127,7 +102,7 @@ async def process_proxies(self):
127
102
Proxy .number_of_bad_checks > 0 ,
128
103
Proxy .number_of_bad_checks < settings .DEAD_PROXY_THRESHOLD ,
129
104
Proxy .last_check_time < time .time () - settings .BAD_PROXY_CHECKING_PERIOD ,
130
- ).order_by (Proxy .last_check_time ).limit (settings .NUMBER_OF_CONCURRENT_TASKS )
105
+ ).order_by (Proxy .last_check_time ).limit (settings .NUMBER_OF_CONCURRENT_TASKS )
131
106
)
132
107
133
108
await self .add_proxies_to_queue (proxies )
@@ -141,7 +116,7 @@ async def process_proxies(self):
141
116
Proxy .number_of_bad_checks >= settings .DEAD_PROXY_THRESHOLD ,
142
117
Proxy .number_of_bad_checks < settings .DO_NOT_CHECK_ON_N_BAD_CHECKS ,
143
118
Proxy .last_check_time < time .time () - settings .DEAD_PROXY_CHECKING_PERIOD ,
144
- ).order_by (Proxy .last_check_time ).limit (settings .NUMBER_OF_CONCURRENT_TASKS )
119
+ ).order_by (Proxy .last_check_time ).limit (settings .NUMBER_OF_CONCURRENT_TASKS )
145
120
)
146
121
147
122
await self .add_proxies_to_queue (proxies )
@@ -179,16 +154,16 @@ async def process_collectors(self):
179
154
180
155
await asyncio .sleep (settings .SLEEP_AFTER_ERROR_PERIOD )
181
156
182
- def is_queue_free (self ):
183
- return self .queue .qsize () < settings .NUMBER_OF_CONCURRENT_TASKS
157
+ async def add_proxy_to_queue (self , proxy : Proxy , collector_id = None ):
158
+ while self .proxies_semaphore .locked ():
159
+ await asyncio .sleep (0.001 )
184
160
185
- async def add_proxy_to_queue (self , proxy : Proxy ):
186
- await self .queue .put ((
161
+ asyncio .ensure_future (self .process_proxy (
187
162
proxy .get_raw_protocol (),
188
163
proxy .auth_data ,
189
164
proxy .domain ,
190
165
proxy .port ,
191
- None
166
+ collector_id ,
192
167
))
193
168
194
169
async def add_proxies_to_queue (self , proxies : list ):
@@ -285,13 +260,13 @@ async def process_raw_proxy(self, proxy, collector_id):
285
260
while not self .good_proxies_are_processed :
286
261
await asyncio .sleep (0.01 )
287
262
288
- await self . queue . put ((
289
- raw_protocol ,
290
- auth_data ,
291
- domain ,
292
- port ,
293
- collector_id ,
294
- ) )
263
+ new_proxy = Proxy ()
264
+ new_proxy . raw_protocol = raw_protocol
265
+ new_proxy . auth_data = auth_data
266
+ new_proxy . domain = domain
267
+ new_proxy . port = port
268
+
269
+ self . add_proxy_to_queue ( new_proxy , collector_id )
295
270
296
271
async def process_proxy (self , raw_protocol : int , auth_data : str , domain : str , port : int , collector_id ):
297
272
async with self .proxies_semaphore :
0 commit comments