Threading - Find Entities
When there is need to resolve many entities, multithreading can be used to speed things up. Below is an example using the autosuggest method and the implementation would be similar with any of the other methods from the knowledge_graph family.
The instance of Bigdata includes a built-in rate limiter to avoid making
too many requests in a short period of time. More information can be
foud at threading
.
Example
Here the goal is to use threads and not having to wait for the response
of KnowledgeGraph.autosuggest
before making the next call. We create a
function autosuggest_handling_errors
that will be used by the threads.
Step 1: Create a function that invokes the method and wraps any possible error.
What to do when an error appears is up to the implementation, in this case, the error message is returned. You may prefer to log it, store it in a file or raise it and stop the execution.
Step 2: Set up the threads and use the function created in step 1.
The function above creates a thread pool and will take turns calling
autosuggest_handling_errors
. The response is returned as a dictionary.
The instance of Bigdata reuses the connection so in case you plan on
creating many threads, increase the number of simultaneous connections
allowed by configuring
BIGDATA_MAX_PARALLEL_REQUESTS
.
Step 3: Execution
Output: