RubyProcess 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0fa90bc235a48025533ac9be8417e8146dc8d14
4
+ data.tar.gz: 6b78bd08325c51d2e1a2e39f182da09a2ff76c2c
5
+ SHA512:
6
+ metadata.gz: ca7b71117f7a38b2e9d41339ea28f775dc0ed2bf719a89600226e36e310807d569544720faa9f10873755355bba5d129a6c52bea334773b06c99441cd004a69a
7
+ data.tar.gz: 24b443cb94c1865e4b651a9bc9f7cebfd925c9b0cbb10b6c4c11f6dffa2e7af50854d147f83bf41b7a4ffe16fe1503ad52a319ceae3cf285bbba9df202bbc196
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "wref"
7
+ gem "tsafe"
8
+ gem "string-cases"
9
+
10
+ # Add dependencies to develop your gem here.
11
+ # Include everything needed to run rake, tests, features, etc.
12
+ group :development do
13
+ gem "rspec", "~> 2.8.0"
14
+ gem "rdoc", "~> 3.12"
15
+ gem "bundler", ">= 1.0.0"
16
+ gem "jeweler", "~> 1.8.3"
17
+ end
18
+
19
+ gem "codeclimate-test-reporter", group: :test, require: nil
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kasper Johansen
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.
@@ -0,0 +1,98 @@
1
+ # RubyProcess
2
+
3
+ Start another Ruby process and manipulate it almost seamlessly.
4
+
5
+ ## Example
6
+
7
+ The CSV lib will not be loaded in the main process and the writing of the file will also take place in another process.
8
+
9
+ ```ruby
10
+ require "rubygems"
11
+ require "ruby_process"
12
+
13
+ RubyProcess.new.spawn_process do |rp|
14
+ rp.static(:Object, :require, "csv")
15
+
16
+ rp.static(:CSV, :open, "test.csv", "w") do |csv|
17
+ csv << ["ID", "Name"]
18
+ csv << [1, "Kasper"]
19
+ end
20
+ end
21
+ ```
22
+
23
+
24
+ ## Install
25
+
26
+ Add to your Gemfile and bundle.
27
+
28
+ ```ruby
29
+ gem "ruby_process"
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ With a block.
35
+
36
+ ```ruby
37
+ RubyProcess.new.spawn_process do |rp|
38
+ rp.static(:File, :open, "some_file", "w") do |fp|
39
+ fp.write("Test!")
40
+ end
41
+ end
42
+ ```
43
+
44
+ Almost seamless mode with ClassProxy.
45
+
46
+ ```ruby
47
+ RubyProcess::ClassProxy.run do |data|
48
+ sp = data[:subproc]
49
+ sp.static(:Object, :require, "tempfile")
50
+
51
+ # Tempfile will be created in the subprocess and not in the current process.
52
+ temp_file = RubyProcess::ClassProxy::Tempfile("temp")
53
+ end
54
+ ```
55
+
56
+ As a variable.
57
+
58
+ ```ruby
59
+ rp = RubyProcess.new(debug: false)
60
+ rp.spawn_process
61
+ test_string = rp.new(:String, "Test")
62
+ ```
63
+
64
+ Calling static methods on classes.
65
+
66
+ ```ruby
67
+ rp.static(:File, :open, "file_path", "w")
68
+ ```
69
+
70
+ Spawning new objects.
71
+
72
+ ```ruby
73
+ file = rp.new(:File, "file_path", "r")
74
+ ```
75
+
76
+ Serializing objects back to the main process.
77
+
78
+ ```ruby
79
+ rp.static(:File, :size, "file_path") #=> RubyProcess::ProxyObject
80
+ rp.static(:File, :size, "file_path").__rp_marshall #=> 2048
81
+ ```
82
+
83
+
84
+ ## Contributing to RubyProcess
85
+
86
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
87
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
88
+ * Fork the project.
89
+ * Start a feature/bugfix branch.
90
+ * Commit and push until you are happy with your contribution.
91
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
92
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
93
+
94
+ ## Copyright
95
+
96
+ Copyright (c) 2012 Kasper Johansen. See LICENSE.txt for
97
+ further details.
98
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "RubyProcess"
18
+ gem.homepage = "http://github.com/kaspernj/ruby_process"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A framework for spawning and communicating with other Ruby-processes}
21
+ gem.description = %Q{A framework for spawning and communicating with other Ruby-processes}
22
+ gem.email = "k@spernj.org"
23
+ gem.authors = ["Kasper Johansen"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task default: :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "RubyProcess #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.12
@@ -0,0 +1,68 @@
1
+ class RubyProcess
2
+ #Calls a block by its block-ID with given arguments.
3
+ def cmd_block_call(obj)
4
+ raise "Invalid block-ID: '#{obj}'." if obj[:block_id].to_i <= 0
5
+ block_ele = @proxy_objs[obj[:block_id]]
6
+ raise "No block by that ID: '#{obj[:block_id]}'." if !block_ele
7
+ raise "Not a block? '#{block_ele.class.name}'." if !block_ele.respond_to?(:call)
8
+ debug "Calling block #{obj[:block_id]}: #{obj}\n" if @debug
9
+
10
+ answer_id = obj[:answer_id]
11
+ raise "No ':answer_id' was given (#{obj})." if !answer_id
12
+
13
+ if answer = @answers[answer_id]
14
+ #Use a queue to sleep thread until the block has been executed.
15
+ queue = Queue.new
16
+ answer.push(type: :proxy_block_call, block: block_ele, args: read_args(obj[:args]), queue: queue)
17
+ res = queue.pop
18
+ raise "Expected true but didnt get that: '#{res}'." if res != true
19
+ else
20
+ block_ele.call(*read_args(obj[:args]))
21
+ end
22
+
23
+ return nil
24
+ end
25
+
26
+ #Spawns a block and returns its ID.
27
+ def cmd_spawn_proxy_block(obj)
28
+ block = proc{
29
+ send(cmd: :block_call, block_id: obj[:id], answer_id: obj[:answer_id])
30
+ }
31
+
32
+ id = block.__id__
33
+ raise "ID already exists: '#{id}'." if @objects.key?(id)
34
+ @objects[id] = block
35
+
36
+ return {id: id}
37
+ end
38
+
39
+ #Dynamically creates a block with a certain arity. If sub-methods measure arity, they will get the correct one from the other process.
40
+ def block_with_arity(args, &block)
41
+ eval_str = "proc{"
42
+ eval_argsarr = "\t\tblock.call("
43
+
44
+ if args[:arity] > 0
45
+ eval_str << "|"
46
+ 1.upto(args[:arity]) do |i|
47
+ if i > 1
48
+ eval_str << ","
49
+ eval_argsarr << ","
50
+ end
51
+
52
+ eval_str << "arg#{i}"
53
+ eval_argsarr << "arg#{i}"
54
+ end
55
+
56
+ eval_str << "|\n"
57
+ eval_argsarr << ")\n"
58
+ end
59
+
60
+ eval_full = eval_str + eval_argsarr
61
+ eval_full << "}"
62
+
63
+ debug "Block eval: #{eval_full}\n" if @debug
64
+ dynamic_proc = eval(eval_full)
65
+
66
+ return dynamic_proc
67
+ end
68
+ end
@@ -0,0 +1,8 @@
1
+ class RubyProcess
2
+ #This command returns an object as a marshalled string, so it can be re-created on the other side.
3
+ def cmd_obj_marshal(obj)
4
+ myobj = @objects[obj[:id]]
5
+ raise "Object by that ID does not exist: '#{obj[:id]}'." if !myobj
6
+ return Marshal.dump(myobj)
7
+ end
8
+ end
@@ -0,0 +1,44 @@
1
+ class RubyProcess
2
+ #Spawns a new object in the process and returns a proxy-object for it.
3
+ def new(classname, *args, &block)
4
+ return send(cmd: :new, classname: classname, args: parse_args(args), &block)
5
+ end
6
+
7
+ #This command spawns a new object of a given class and returns its hash-handle, so a proxy-object can be spawned on the other side.
8
+ def cmd_new(obj)
9
+ const = obj[:classname].to_s.split("::").inject(Object, :const_get)
10
+
11
+ debug "New-args-before: #{obj[:args]}\n" if @debug
12
+ real_args = read_args(obj[:args])
13
+ debug "New-args-after: #{real_args}\n" if @debug
14
+
15
+ retobj = const.new(*real_args)
16
+ return handle_return_object(retobj)
17
+ end
18
+
19
+ #Calls a method on an object.
20
+ def cmd_obj_method(obj)
21
+ myobj = @objects[obj[:id]]
22
+ raise "Object by that ID does not exist: '#{obj[:id]}' '(#{@objects.keys.sort.join(",")})." if !myobj
23
+
24
+ if obj.key?(:block)
25
+ real_block = proc{|*args|
26
+ debug "Block called! #{args}\n" if @debug
27
+ send(cmd: :block_call, block_id: obj[:block][:id], answer_id: obj[:send_id], args: handle_return_args(args))
28
+ }
29
+
30
+ block = block_with_arity(arity: obj[:block][:arity], &real_block)
31
+ debug "Spawned fake block with arity: #{block.arity}\n" if @debug
32
+ else
33
+ block = nil
34
+ end
35
+
36
+ debug "Obj-method-args-before: #{obj[:args]}\n" if @debug
37
+ real_args = read_args(obj[:args])
38
+ debug "Obj-methods-args-after: #{real_args}\n" if @debug
39
+
40
+ debug "Calling #{myobj.class.name}.#{obj[:method]}(*#{obj[:args]}, &#{block})\n" if @debug
41
+ res = myobj.__send__(obj[:method], *real_args, &block)
42
+ return handle_return_object(res)
43
+ end
44
+ end
@@ -0,0 +1,14 @@
1
+ class RubyProcess
2
+ #Returns a numeric value like a integer. This methods exists because it isnt possible to do: "Integer.new(5)".
3
+ #===Examples
4
+ # proxy_int = rp.numeric(5)
5
+ # proxy_int.__rp_marshal #=> 5
6
+ def numeric(val)
7
+ return send(cmd: :numeric, val: val)
8
+ end
9
+
10
+ #Process-method for the 'numeric'-method.
11
+ def cmd_numeric(obj)
12
+ return handle_return_object(obj[:val].to_i)
13
+ end
14
+ end
@@ -0,0 +1,36 @@
1
+ class RubyProcess
2
+ #Calls a static method on the process-side.
3
+ #===Examples
4
+ # rp.static(:File, :open, "/tmp/somefile", "w") do |fp|
5
+ # fp.write("Test")
6
+ # end
7
+ def static(classname, method, *args, &block)
8
+ debug "Args-before: #{args} (#{@my_pid})\n" if @debug
9
+ real_args = parse_args(args)
10
+ debug "Real-args: #{real_args}\n" if @debug
11
+
12
+ return send(cmd: :static, classname: classname, method: method, args: real_args, &block)
13
+ end
14
+
15
+ #Process-method for the 'static'-method.
16
+ def cmd_static(obj)
17
+ if obj.key?(:block)
18
+ real_block = proc{|*args|
19
+ debug "Block called! #{args}\n" if @debug
20
+ send(cmd: :block_call, block_id: obj[:block][:id], answer_id: obj[:send_id], args: handle_return_args(args))
21
+ }
22
+
23
+ block = block_with_arity(arity: obj[:block][:arity], &real_block)
24
+ else
25
+ block = nil
26
+ end
27
+
28
+ debug "Static-args-before: #{obj[:args]}\n" if @debug
29
+ real_args = read_args(obj[:args])
30
+ debug "Static-args-after: #{real_args}\n" if @debug
31
+
32
+ const = obj[:classname].to_s.split("::").inject(Object, :const_get)
33
+ retobj = const.__send__(obj[:method], *real_args, &block)
34
+ return handle_return_object(retobj)
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ class RubyProcess
2
+ #Evalulates the given string in the process.
3
+ #===Examples
4
+ # rp.str_eval("return 10").__rp_marshal #=> 10
5
+ def str_eval(str)
6
+ send(cmd: :str_eval, str: str)
7
+ end
8
+
9
+ #Process-method for 'str_eval'.
10
+ def cmd_str_eval(obj)
11
+ #Lamda is used here because 'return' might be used in evalled code and thereby return an unhandeled object.
12
+ return handle_return_object(lambda{
13
+ eval(obj[:str])
14
+ }.call)
15
+ end
16
+ end
@@ -0,0 +1,47 @@
1
+ class RubyProcess
2
+ #Closes the process by executing exit.
3
+ def cmd_exit(obj)
4
+ exit
5
+ end
6
+
7
+ #Flushes various collected object-IDs to the subprocess, where they will be garbage-collected.
8
+ def flush_finalized
9
+ @flush_mutex.synchronize do
10
+ debug "Ruby-process-debug: Checking finalized\n" if @debug
11
+ ids = @proxy_objs_unsets.shift(500)
12
+ debug "IDS: #{ids} #{@proxy_objs_unsets}\n" if @debug
13
+ return nil if ids.empty?
14
+
15
+ debug "Ruby-process-debug: Finalizing (#{ids}).\n" if @debug
16
+ send(cmd: :flush_finalized, ids: ids)
17
+ @finalize_count += ids.length
18
+ return nil
19
+ end
20
+ end
21
+
22
+ #Flushes references to the given object IDs.
23
+ def cmd_flush_finalized(obj)
24
+ debug "Command-flushing finalized: '#{obj[:ids]}'.\n" if @debug
25
+ obj[:ids].each do |id|
26
+ raise "Unknown ID: '#{id}' (#{id.class.name})." unless @objects.key?(id)
27
+ @objects.delete(id)
28
+ end
29
+
30
+ return nil
31
+ end
32
+
33
+ #Starts garbage-collecting and then flushes the finalized objects to the sub-process. Does the same thing in the sub-process.
34
+ def garbage_collect
35
+ GC.start
36
+ self.flush_finalized
37
+ send(cmd: :garbage_collect)
38
+ return nil
39
+ end
40
+
41
+ #The sub-process-side execution of 'garbage_collect'.
42
+ def cmd_garbage_collect(obj)
43
+ GC.start
44
+ self.flush_finalized
45
+ return nil
46
+ end
47
+ end