You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example runs a search for "coffee" on Google. It then returns the results as a regular Ruby Hash. See the [playground](https://serpapi.com/playground) to generate your own code.
47
+
This example runs a search for "coffee" on Google. It then returns the results as a regular Ruby Hash.
48
+
See the [playground](https://serpapi.com/playground) to generate your own code.
49
+
50
+
The `SERPAPI_KEY` should be replaced with your actual API key obtained from
Search API features non-blocking search using the option: `async=true`.
114
+
- Non-blocking - async=true - a single parent process can handle unlimited concurrent searches.
115
+
- Blocking - async=false - many processes must be forked and synchronized to handle concurrent searches. This strategy is I/O usage because each client would hold a network connection.
116
+
117
+
Search API enables `async` search.
118
+
- Non-blocking (`async=true`) : the development is more complex, but this allows handling many simultaneous connections.
119
+
- Blocking (`async=false`) : it's easy to write the code but more compute-intensive when the parent process needs to hold many connections.
120
+
121
+
Here is an example of asynchronous searches using Ruby
122
+
```ruby
123
+
require'serpapi'
124
+
125
+
company_list =%w(meta amazon apple netflix google)
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).
159
+
Each search takes a few seconds before completion by SerpApi service and the search engine. By the time the first element pops out of the queue. The search result might be already available in the archive. If not, the `search_archive` method blocks until the search results are available.
160
+
161
+
### Search at scale
162
+
The provided code snippet is a Ruby spec test case that demonstrates the use of thread pools to execute multiple HTTP requests concurrently.
163
+
164
+
```ruby
165
+
require'serpapi'
166
+
require'connection_pool'
167
+
168
+
# create a thread pool of 4 threads with a persistent connection to serpapi.com
@@ -162,8 +295,6 @@ It prints your account information.
162
295
163
296
## Basic example per search engine
164
297
165
-
Here is how to calls the APIs.
166
-
167
298
### Search google
168
299
```ruby
169
300
require'serpapi'
@@ -786,102 +917,14 @@ Most notable improvements:
786
917
- Reduce logic complexity in our implementation. (faster performance)
787
918
- Better documentation.
788
919
789
-
## Advanced search API usage
790
-
### Highly scalable batching
791
-
792
-
Search API features non-blocking search using the option: `async=true`.
793
-
- Non-blocking - async=true - a single parent process can handle unlimited concurrent searches.
794
-
- Blocking - async=false - many processes must be forked and synchronized to handle concurrent searches. This strategy is I/O usage because each client would hold a network connection.
795
-
796
-
Search API enables `async` search.
797
-
- Non-blocking (`async=true`) : the development is more complex, but this allows handling many simultaneous connections.
798
-
- Blocking (`async=false`) : it's easy to write the code but more compute-intensive when the parent process needs to hold many connections.
799
-
800
-
Here is an example of asynchronous searches using Ruby
801
-
```ruby
802
-
require'serpapi'
803
-
# The code snippet aims to improve the efficiency of searching using the SerpApi client async function. It
804
-
# targets companies in the MAANG (Meta, Amazon, Apple, Netflix, Google) group.
805
-
#
806
-
# **Process:**
807
-
# 1. **Request Queue:** The company list is iterated over, and each company is queried using the SerpApi client. Requests
808
-
# are stored in a queue to avoid blocking the main thread.
809
-
#
810
-
# 2. **Client Retrieval:** After each request, the code checks the status of the search result. If it's cached or
811
-
# successful, the company name is printed, and the request is skipped. Otherwise, the result is added to the queue for
812
-
# further processing.
813
-
#
814
-
# 3. **Queue Processing:** The queue is processed until it's empty. In each iteration, the last result is retrieved and
815
-
# its client ID is extracted.
816
-
#
817
-
# 4. **Archived Client Retrieval:** Using the client ID, the code retrieves the archived client and checks its status. If
818
-
# it's cached or successful, the company name is printed, and the client is skipped. Otherwise, the result is added back
819
-
# to the queue for further processing.
820
-
#
821
-
# 5. **Completion:** The queue is closed, and a message is printed indicating that the process is complete.
822
-
#
823
-
# * **Asynchronous Requests:** The `async: true` option ensures that search requests are processed in parallel, improving
824
-
# efficiency.
825
-
# * **Queue Management:** The queue allows requests to be processed asynchronously without blocking the main thread.
826
-
# * **Status Checking:** The code checks the status of each search result before processing it, avoiding unnecessary work.
827
-
# * **Queue Processing:** The queue ensures that all requests are processed in the order they were submitted.
828
-
829
-
# **Overall, the code snippet demonstrates a well-structured approach to improve the efficiency of searching for company
830
-
# information using SerpApi.**
831
-
832
-
# load serpapi library
833
-
require'serpapi'
834
-
835
-
# target MAANG companies
836
-
company_list =%w(meta amazon apple netflix google)
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).
875
-
Each search takes a few seconds before completion by SerpApi service and the search engine. By the time the first element pops out of the queue. The search result might be already available in the archive. If not, the `search_archive` method blocks until the search results are available.
0 commit comments