simple-queue 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +131 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/simple/queue.rb +39 -0
- data/lib/simple/queue/version.rb +5 -0
- data/simple-queue.gemspec +23 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6c13aed10884e8b79ab2ba86b505dee7542867e7
|
4
|
+
data.tar.gz: c5f49d232c1ce7e2d8fa6b79405af9a713f283fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3e9b6ca1d32e7f79b632aef4ec1de9ee688e091845d9d58dd9881aa1f9b6df263ea21b29a2652cce37a7c33f4ea9a39aa96d2dc9c83e996bb697a10abee4526
|
7
|
+
data.tar.gz: 23165a9c8a98fb0103e3bf7b9e72e51e6571d2c7a9483fbdd04a07277bbcc346edb1e951ada9f8dd70e5784bb6d474b3676e20d36ddb7c65990d72cec4b54d12
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
# Simple::Queue
|
2
|
+
|
3
|
+
`Simple::Queue` is a Ruby library that provides a non-blocking queue similar to
|
4
|
+
the `Queue` library provided in Ruby's standard library. The difference is that
|
5
|
+
this queue does not require jumping through hoops for non-blocking behavior.
|
6
|
+
|
7
|
+
An example:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
require 'queue'
|
11
|
+
|
12
|
+
# Ruby `Queue` blocking example:
|
13
|
+
queue = Queue.new
|
14
|
+
queue << 'foo'
|
15
|
+
queue << 'bar'
|
16
|
+
queue.pop #=> 'foo'
|
17
|
+
queue.pop #=> 'bar'
|
18
|
+
queue.pop # Blocks until an item is pushed onto the queue from another thread.
|
19
|
+
|
20
|
+
# Ruby `Queue` non-blocking example:
|
21
|
+
queue = Queue.new
|
22
|
+
queue << 'foo'
|
23
|
+
queue << 'bar'
|
24
|
+
queue.pop(true) #=> 'foo'
|
25
|
+
queue.pop(true) #=> 'bar'
|
26
|
+
queue.pop(true) # Raises a `ThreadError`.
|
27
|
+
|
28
|
+
# Alternative approach:
|
29
|
+
queue = Queue.new
|
30
|
+
queue << 'foo'
|
31
|
+
queue << 'bar'
|
32
|
+
queue.pop(true) rescue nil #=> 'foo'
|
33
|
+
queue.pop(true) rescue nil #=> 'bar'
|
34
|
+
queue.pop(true) rescue nil #=> nil
|
35
|
+
queue.pop(true) rescue nil #=> nil
|
36
|
+
```
|
37
|
+
|
38
|
+
Whereas with `Simple::Queue`:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'simple/queue'
|
42
|
+
|
43
|
+
queue = Simple::Queue.new
|
44
|
+
queue << 'foo'
|
45
|
+
queue << 'bar'
|
46
|
+
queue.pop #=> 'foo'
|
47
|
+
queue.pop #=> 'bar'
|
48
|
+
queue.pop #=> nil
|
49
|
+
queue.pop #=> nil
|
50
|
+
```
|
51
|
+
|
52
|
+
In every other way, `Simple::Queue` acts like the Ruby queue, with the caveat
|
53
|
+
that it will be minutely slower in the best case (since it is pure Ruby and not
|
54
|
+
C), and faster in the worst case (since there is no rescuing of exceptions,
|
55
|
+
which is slow in Ruby).
|
56
|
+
|
57
|
+
It is thread-safe, since all operations are wrapped in a Mutex. But since it
|
58
|
+
does not block, there should be no opportunity for race conditions (every
|
59
|
+
operation performs exactly one thing and returns).
|
60
|
+
|
61
|
+
It follows the API of `Queue` exactly:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# Create a new simple-queue.
|
65
|
+
Queue#initialize
|
66
|
+
|
67
|
+
# Clear all items out of the queue.
|
68
|
+
Queue#clear
|
69
|
+
|
70
|
+
# Returns `true` if there is nothing in the queue, and `false` otherwise.
|
71
|
+
Queue#empty?
|
72
|
+
|
73
|
+
# Returns how many items are in the queue.
|
74
|
+
Queue#length
|
75
|
+
Queue#size
|
76
|
+
|
77
|
+
# Puts an item in the queue.
|
78
|
+
Queue#push(obj)
|
79
|
+
Queue#<<(obj)
|
80
|
+
Queue#enq(obj)
|
81
|
+
|
82
|
+
# Removes an item from the queue, or returns `nil` if there are none. This
|
83
|
+
# method takes an optional argument, which is discarded. This is for
|
84
|
+
# compatibility with Ruby's `Queue` implementation.
|
85
|
+
#
|
86
|
+
Queue#pop
|
87
|
+
Queue#deq
|
88
|
+
Queue#shift
|
89
|
+
|
90
|
+
# Always returns `0`, since there is no opportunity to wait.
|
91
|
+
Queue#num_waiting
|
92
|
+
```
|
93
|
+
|
94
|
+
## Installation
|
95
|
+
|
96
|
+
Add this line to your application's Gemfile:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
gem 'simple-queue'
|
100
|
+
```
|
101
|
+
|
102
|
+
And then execute:
|
103
|
+
|
104
|
+
$ bundle
|
105
|
+
|
106
|
+
Or install it yourself as:
|
107
|
+
|
108
|
+
$ gem install simple-queue
|
109
|
+
|
110
|
+
## Development
|
111
|
+
|
112
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
113
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
114
|
+
prompt that will allow you to experiment.
|
115
|
+
|
116
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
117
|
+
release a new version, update the version number in `version.rb`, and then run
|
118
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
119
|
+
git commits and tags, and push the `.gem` file to
|
120
|
+
[rubygems.org](https://rubygems.org).
|
121
|
+
|
122
|
+
## TODO
|
123
|
+
|
124
|
+
* Document code with RDoc.
|
125
|
+
* More exhaustively test threading in test suite.
|
126
|
+
|
127
|
+
## Contributing
|
128
|
+
|
129
|
+
Bug reports and pull requests are welcome on GitHub at
|
130
|
+
https://github.com/rjungemann/simple-queue.
|
131
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "simple/queue"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/simple/queue.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "simple/queue/version"
|
2
|
+
|
3
|
+
module Simple
|
4
|
+
class Queue
|
5
|
+
def initialize
|
6
|
+
@array = []
|
7
|
+
@mutex = Mutex.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def clear
|
11
|
+
@mutex.synchronize { @array = [] }
|
12
|
+
end
|
13
|
+
|
14
|
+
def empty?
|
15
|
+
@mutex.synchronize { @array.empty? }
|
16
|
+
end
|
17
|
+
|
18
|
+
def length
|
19
|
+
@mutex.synchronize { @array.length }
|
20
|
+
end
|
21
|
+
alias_method :size, :length
|
22
|
+
|
23
|
+
def push(obj)
|
24
|
+
@mutex.synchronize { @array.push(obj) }
|
25
|
+
end
|
26
|
+
alias_method :<<, :push
|
27
|
+
alias_method :enq, :push
|
28
|
+
|
29
|
+
def pop(_=nil)
|
30
|
+
@mutex.synchronize { @array.shift }
|
31
|
+
end
|
32
|
+
alias_method :deq, :pop
|
33
|
+
alias_method :shift, :pop
|
34
|
+
|
35
|
+
def num_waiting
|
36
|
+
0
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple/queue/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple-queue"
|
8
|
+
spec.version = Simple::Queue::VERSION
|
9
|
+
spec.authors = ["Roger Jungemann"]
|
10
|
+
spec.email = ["roger@thefifthcircuit.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A dead-simple non-blocking queue.}
|
13
|
+
spec.homepage = "https://github.com/rjungemann/simple-queue"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-queue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roger Jungemann
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- roger@thefifthcircuit.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/simple/queue.rb
|
71
|
+
- lib/simple/queue/version.rb
|
72
|
+
- simple-queue.gemspec
|
73
|
+
homepage: https://github.com/rjungemann/simple-queue
|
74
|
+
licenses: []
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.5.1
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: A dead-simple non-blocking queue.
|
96
|
+
test_files: []
|