object-threads 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +16 -0
- data/LICENSE.txt +20 -0
- data/README.md +56 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/lib/thread/object.rb +93 -0
- data/test.rb +16 -0
- metadata +89 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "depq", ">= 0.4"
|
5
|
+
# gem "hash-utils", ">= 0.7.0"
|
6
|
+
|
7
|
+
# Add dependencies to develop your gem here.
|
8
|
+
# Include everything needed to run rake, tests, features, etc.
|
9
|
+
group :development do
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.2"
|
12
|
+
end
|
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 - 2011 Martin Kozák (martinkozak@martinkozak.net)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Object Threads
|
2
|
+
==============
|
3
|
+
|
4
|
+
**Object Threads** emulates Python threading API paradigm. Creating
|
5
|
+
threads by giving some block to constructor is sometime slightly
|
6
|
+
unpractical way of running threads. This library allows encapsulating
|
7
|
+
threads to classes, so class can define itself as thread which can be
|
8
|
+
run by plain method call.
|
9
|
+
|
10
|
+
For example:
|
11
|
+
|
12
|
+
require "thread/object"
|
13
|
+
|
14
|
+
class Foo
|
15
|
+
include Thread::Object
|
16
|
+
|
17
|
+
def run
|
18
|
+
sleep 10
|
19
|
+
puts "Something in thread."
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Foo::new.start!
|
24
|
+
sleep 5
|
25
|
+
|
26
|
+
puts "Something in main."
|
27
|
+
|
28
|
+
Will print out `"Something in main."` and then `"Something in thread."`.
|
29
|
+
|
30
|
+
Thread object module directly supports `#alive?` and `#shutdown!`
|
31
|
+
methods only for thread manipulation. For other operations with the
|
32
|
+
thread, call directly object's `#native_thread` property which contains
|
33
|
+
appropriate native [Ruby Thread][1] object. This setup is for avoiding
|
34
|
+
potential method name conflicts between module and user class.
|
35
|
+
|
36
|
+
|
37
|
+
Contributing
|
38
|
+
------------
|
39
|
+
|
40
|
+
1. Fork it.
|
41
|
+
2. Create a branch (`git checkout -b 20101220-my-change`).
|
42
|
+
3. Commit your changes (`git commit -am "Added something"`).
|
43
|
+
4. Push to the branch (`git push origin 20101220-my-change`).
|
44
|
+
5. Create an [Issue][2] with a link to your branch.
|
45
|
+
6. Enjoy a refreshing Diet Coke and wait.
|
46
|
+
|
47
|
+
|
48
|
+
Copyright
|
49
|
+
---------
|
50
|
+
|
51
|
+
Copyright © 2010-2011 [Martin Kozák][3]. See `LICENSE.txt` for
|
52
|
+
further details.
|
53
|
+
|
54
|
+
[1]: http://www.ruby-doc.org/core/classes/Thread.html
|
55
|
+
[2]: http://github.com/martinkozak/qrpc/issues
|
56
|
+
[3]: http://www.martinkozak.net/
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler'
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
require 'rake'
|
12
|
+
|
13
|
+
require 'jeweler'
|
14
|
+
Jeweler::Tasks.new do |gem|
|
15
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
16
|
+
gem.name = "object-threads"
|
17
|
+
gem.homepage = "http://github.com/martinkozak/object-threads"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = 'Emulates Python threading API paradigm. Creating threads by giving some block to constructor is sometime slightly unpractical way of running threads. This library allows encapsulating threads to classes, so class can define itself as thread which can be run by plain method call.'
|
20
|
+
gem.email = "martinkozak@martinkozak.net"
|
21
|
+
gem.authors = ["Martin Kozák"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/rdoctask'
|
30
|
+
Rake::RDocTask.new do |rdoc|
|
31
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
32
|
+
|
33
|
+
rdoc.rdoc_dir = 'rdoc'
|
34
|
+
rdoc.title = "qrpc #{version}"
|
35
|
+
rdoc.rdoc_files.include('README*')
|
36
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
37
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2010-2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
require "thread"
|
5
|
+
|
6
|
+
##
|
7
|
+
# Standard Ruby's +Thread+ class.
|
8
|
+
#
|
9
|
+
|
10
|
+
class Thread
|
11
|
+
|
12
|
+
##
|
13
|
+
# Represents Python-style class interfacing thread.
|
14
|
+
#
|
15
|
+
|
16
|
+
module Object
|
17
|
+
|
18
|
+
##
|
19
|
+
# Holds Ruby native thread instance.
|
20
|
+
# @return [Thread] Ruby native thread object
|
21
|
+
#
|
22
|
+
|
23
|
+
attr_accessor :native_thread
|
24
|
+
@native_thread
|
25
|
+
|
26
|
+
##
|
27
|
+
# Runs the thread code.
|
28
|
+
# @abstract
|
29
|
+
#
|
30
|
+
|
31
|
+
def run
|
32
|
+
raise Exception::new("Method #run must be overriden. It should contain body of the thread.")
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Starts the thread by calling the +#run+ method as in Python.
|
37
|
+
# It's non-blocking, of sure.
|
38
|
+
#
|
39
|
+
# Uncacthed rxceptions raised by thread are written out to
|
40
|
+
# +STDERR+ by default. This feature can be turned of by the
|
41
|
+
# +silent+ argument.
|
42
|
+
#
|
43
|
+
# @param [nil, :silent] silent indicates, it shouln't write
|
44
|
+
# exceptions out
|
45
|
+
# @return [Thread] native Ruby thread
|
46
|
+
# @see #run
|
47
|
+
#
|
48
|
+
|
49
|
+
def start!(silent = nil)
|
50
|
+
@native_thread = Thread::new do
|
51
|
+
begin
|
52
|
+
self.run()
|
53
|
+
rescue ::Exception => e
|
54
|
+
if silent != :silent
|
55
|
+
self.log "THREAD EXCEPTION! " << e.class.to_s << ": " << e.message
|
56
|
+
e.backtrace.each { |i| self.log i }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
return @native_thread
|
62
|
+
end
|
63
|
+
|
64
|
+
##
|
65
|
+
# Shutdowns the thread.
|
66
|
+
#
|
67
|
+
|
68
|
+
def shutdown!
|
69
|
+
@native_thread.terminate()
|
70
|
+
end
|
71
|
+
|
72
|
+
##
|
73
|
+
# Indicates thread is alive.
|
74
|
+
# @param [Boolean] +true+ if it is, +false+ in otherwise
|
75
|
+
#
|
76
|
+
|
77
|
+
def alive?
|
78
|
+
@native_thread.alive?
|
79
|
+
end
|
80
|
+
|
81
|
+
##
|
82
|
+
# Logs an message.
|
83
|
+
# By overriding this, you can change the format.
|
84
|
+
#
|
85
|
+
# @param [String] message message for writing out
|
86
|
+
#
|
87
|
+
|
88
|
+
def log(message)
|
89
|
+
STDERR.write "[" << Time.now.strftime("%Y-%m-%d %H:%M:%S") << "] " << self.class.name << ": " << message.to_s << "\n"
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
data/test.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: object-threads
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- "Martin Koz\xC3\xA1k"
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-02-13 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: bundler
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.0
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jeweler
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.5.2
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
description:
|
39
|
+
email: martinkozak@martinkozak.net
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files:
|
45
|
+
- LICENSE.txt
|
46
|
+
- README.md
|
47
|
+
files:
|
48
|
+
- .document
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- VERSION
|
55
|
+
- lib/thread/object.rb
|
56
|
+
- test.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/martinkozak/object-threads
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: -1958335550909864169
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.5.2
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Emulates Python threading API paradigm. Creating threads by giving some block to constructor is sometime slightly unpractical way of running threads. This library allows encapsulating threads to classes, so class can define itself as thread which can be run by plain method call.
|
88
|
+
test_files: []
|
89
|
+
|