Sha256: 7e71981e25c7852bf869525a5e30ee5916ed6996a9ac070fb6ae28947cb95c97

Contents?: true

Size: 1.32 KB

Versions: 28

Compression:

Stored size: 1.32 KB

Contents

A synchronization primitive, which limits access to a given resource, such as a limited number of database connections, open files, or network connections.

## Example

~~~ ruby
require 'async'
require 'async/semaphore'
require 'net/http'

Sync do
	# Only allow two concurrent tasks at a time:
	semaphore = Async::Semaphore.new(2)
	
	# Generate an array of 10 numbers:
	terms = ['ruby', 'python', 'go', 'java', 'c++'] 
	
	# Search for the terms:
	terms.each do |term|
		semaphore.async do |task|
			Console.logger.info("Searching for #{term}...")
			response = Net::HTTP.get(URI "https://www.google.com/search?q=#{term}")
			Console.logger.info("Got response #{response.size} bytes.")
		end
	end
end
~~~

### Output

~~~
0.0s     info: Searching for ruby... [ec=0x3c] [pid=50523]
0.04s     info: Searching for python... [ec=0x21c] [pid=50523]
1.7s     info: Got response 182435 bytes. [ec=0x3c] [pid=50523]
1.71s     info: Searching for go... [ec=0x834] [pid=50523]
3.0s     info: Got response 204854 bytes. [ec=0x21c] [pid=50523]
3.0s     info: Searching for java... [ec=0xf64] [pid=50523]
4.32s     info: Got response 103235 bytes. [ec=0x834] [pid=50523]
4.32s     info: Searching for c++... [ec=0x12d4] [pid=50523]
4.65s     info: Got response 109697 bytes. [ec=0xf64] [pid=50523]
6.64s     info: Got response 87249 bytes. [ec=0x12d4] [pid=50523]
~~~

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
async-2.3.0 lib/async/semaphore.md
async-2.2.1 lib/async/semaphore.md
async-2.2.0 lib/async/semaphore.md
async-2.1.0 lib/async/semaphore.md
async-2.0.3 lib/async/semaphore.md
async-2.0.2 lib/async/semaphore.md
async-2.0.1 lib/async/semaphore.md
async-2.0.0 lib/async/semaphore.md