[fix] thread safe threaded_requests() function
This commit is contained in:
		
							parent
							
								
									b816752719
								
							
						
					
					
						commit
						55abf07a4f
					
				| @ -19,6 +19,7 @@ import gc | |||||||
| import threading | import threading | ||||||
| from thread import start_new_thread | from thread import start_new_thread | ||||||
| from time import time | from time import time | ||||||
|  | from uuid import uuid4 | ||||||
| import searx.poolrequests as requests_lib | import searx.poolrequests as requests_lib | ||||||
| from searx.engines import ( | from searx.engines import ( | ||||||
|     categories, engines |     categories, engines | ||||||
| @ -56,19 +57,20 @@ def search_request_wrapper(fn, url, engine_name, **kwargs): | |||||||
| def threaded_requests(requests): | def threaded_requests(requests): | ||||||
|     timeout_limit = max(r[2]['timeout'] for r in requests) |     timeout_limit = max(r[2]['timeout'] for r in requests) | ||||||
|     search_start = time() |     search_start = time() | ||||||
|  |     search_id = uuid4().__str__() | ||||||
|     for fn, url, request_args, engine_name in requests: |     for fn, url, request_args, engine_name in requests: | ||||||
|         request_args['timeout'] = timeout_limit |         request_args['timeout'] = timeout_limit | ||||||
|         th = threading.Thread( |         th = threading.Thread( | ||||||
|             target=search_request_wrapper, |             target=search_request_wrapper, | ||||||
|             args=(fn, url, engine_name), |             args=(fn, url, engine_name), | ||||||
|             kwargs=request_args, |             kwargs=request_args, | ||||||
|             name='search_request', |             name=search_id, | ||||||
|         ) |         ) | ||||||
|         th._engine_name = engine_name |         th._engine_name = engine_name | ||||||
|         th.start() |         th.start() | ||||||
| 
 | 
 | ||||||
|     for th in threading.enumerate(): |     for th in threading.enumerate(): | ||||||
|         if th.name == 'search_request': |         if th.name == search_id: | ||||||
|             remaining_time = max(0.0, timeout_limit - (time() - search_start)) |             remaining_time = max(0.0, timeout_limit - (time() - search_start)) | ||||||
|             th.join(remaining_time) |             th.join(remaining_time) | ||||||
|             if th.isAlive(): |             if th.isAlive(): | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Adam Tauber
						Adam Tauber