Skip to content

Commit a8937b2

Browse files
authored
Update README.md
polish advanced examples
1 parent 548a166 commit a8937b2

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

README.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,24 @@ search_queue.close
153153
puts 'done'
154154
```
155155

156-
* source code: [demo/demo_async.rb](https://github.com/serpapi/serpapi-ruby/blob/master/demo/demo_async.rb)
157-
158156
This code shows a simple solution to batch searches asynchronously into a [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)).
159157
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.
160158

159+
* source code: [demo/demo_async.rb](https://github.com/serpapi/serpapi-ruby/blob/master/demo/demo_async.rb)
160+
161161
### 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.
162+
163+
The code aims to demonstrate how thread pools can be used to improve performance by executing multiple search concurrently.
164+
The connection_pool gem abstract away the management of a pool of connections (must be installed using `gem install connection_pool`)
163165

164166
```ruby
165167
require 'serpapi'
166168
require 'connection_pool'
167169

168-
# create a thread pool of 4 threads with a persistent connection to serpapi.com
170+
# create 4 persistent connection to serpapi.com
171+
n = 4
169172
pool = ConnectionPool.new(size: n, timeout: 5) do
170-
SerpApi::Client.new(engine: 'google', api_key: ENV['API_KEY'], timeout: 30, persistent: true)
173+
SerpApi::Client.new(engine: 'google', api_key: ENV['SERPAPI_KEY'], timeout: 30, persistent: true)
171174
end
172175

173176
# run user thread to search for your favorites coffee type
@@ -179,24 +182,18 @@ end
179182
responses = threads.map(&:value)
180183
```
181184

182-
The code aims to demonstrate how thread pools can be used to
183-
improve performance by executing multiple tasks concurrently. In
184-
this case, it makes multiple HTTP requests to an API endpoint using
185-
a thread pool of persistent connections.
186-
187185
**Benefits:**
188186

189-
* Improved performance by avoiding the overhead of creating and
190-
destroying connections for each request.
191-
* Efficient use of resources by sharing connections among multiple
192-
threads.
193-
* Concurrency and parallelism, allowing multiple requests to be
194-
processed simultaneously.
187+
* Improved performance by avoiding the overhead of creating and destroying connections for each request.
188+
* Efficient use of resources by sharing connections among multiple threads.
189+
* Concurrency and parallelism, allowing multiple requests to be processed simultaneously.
195190

196-
benchmark: (demo/demo_thread_pool.rb)
191+
source code: [demo/demo_thread_pool.rb](https://github.com/serpapi/serpapi-ruby/blob/master/demo/demo_thread_pool.rb)
197192

198193
### Real world search without persistency
199194

195+
when a client performs a single search
196+
200197
```ruby
201198
require 'serpapi'
202199

0 commit comments

Comments
 (0)