""" Wiki Page Titles """ Dictionary to cache search results searchcache = {} wikititles…
- User
""" Wiki Page Titles """
Dictionary to cache search results
search_cache = {}
wiki_titles = []
Helper function to perform a single API call
def search_wrapper(term): if term in search_cache: return search_cache[term] else: search_result = wp.search(term, results=250, suggestion=True) search_cache[term] = search_result time.sleep(0.1) # Add a small delay to prevent rate limiting return search_result
Batch size for API calls
batch_size = 25
Process search terms in batches using ThreadPoolExecutor
def process_batch(batch_search_terms): with ThreadPoolExecutor() as executor: search_results = list(executor.map(search_wrapper, batch_search_terms))
for search in search_results: if search[1] is not None: search2 = wp.search(search[1], results=250) unique_list = list(set(search[0] + search2)) else: unique_list = list(set(search[0])) wiki_titles.append(unique_list)Process search terms in batches
for i in tqdm(range(0, len(dtic_list), batch_size)): batch_search_terms = dtic_list[i:i + batch_size] process_batch(batch_search_terms)
Using list comprehension
wiki_titles_flat = [item for sublist in wiki_titles for item in sublist]
Ensure unique
wiki_titles_unique = list(set(wiki_titles_flat))
does this use all my cpu resources?
- Assistant
No, this code does not use all your CPU resources. It uses a ThreadPoolExecutor to process search terms in parallel, which can help utilize multiple CPU cores efficiently. However, the number of threads in the executor is not explicitly specified, so it will use a default value based on your system's capabilities. If you want more control over the number of threads being used, you can explicitly set the max_workers parameter when creating the ThreadPoolExecutor.
- User
how do i set max_workers
- Assistant
To set the
max_workersparameter when creating theThreadPoolExecutor, you need to specify the desired number of threads. Here's an example of how you can modify the code to setmax_workersto your preferred value:from concurrent.futures import ThreadPoolExecutor # ... # Process search terms in batches using ThreadPoolExecutor def process_batch(batch_search_terms): with ThreadPoolExecutor(max_workers=4) as executor: # Set max_workers to desired number of threads (e.g., 4) search_results = list(executor.map(search_wrapper, batch_search_terms)) for search in search_results: # ...In the above example,
max_workersis set to 4, which means theThreadPoolExecutorwill use 4 threads to process the search terms concurrently. You can adjust this value based on the number of CPU cores available and your system's capabilities.