multiprocess-threads 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/multiprocess-threads.rb +69 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 437522eb4401d6c4e0ed56f5895bf5ab6d1d29130619a32743db3894470757d6
4
+ data.tar.gz: d180a170c09cd6212b6530ce36c61663a85f974071206af71bee1262b78255a8
5
+ SHA512:
6
+ metadata.gz: 4286f37714a22d3af95ae0dd67f1ea956f642e69f5cd3dc8f67125116e1bf53d94ec5a4b67dde4faf639763b32ffb54c875d2fe0e8db70e255c48baf25ba7a8e
7
+ data.tar.gz: 70be491ecc2a8abff946097383d510326a46bb0197cdfd50428c0d19e652e5cd6a9c1c3fb1dfb7295f9d4ea3c0b4e47897c5fd0c49e7555aede251a828057b29
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'etc'
5
+ require 'English'
6
+
7
+ module MPThreads
8
+ # Creates one way channel
9
+ class Channel
10
+ def initialize
11
+ @read_io, @write_io = IO.pipe
12
+ end
13
+
14
+ def read
15
+ @write_io.close unless @write_io.closed?
16
+ data = @read_io.gets
17
+ return nil unless data
18
+
19
+ Marshal.load data # rubocop:disable Security/MarshalLoad
20
+ end
21
+
22
+ def write(data)
23
+ @read_io.close unless @read_io.closed?
24
+ @write_io << "#{Marshal.dump(data)}#{$RS}"
25
+ end
26
+ end
27
+
28
+ # Run tasks using multiple processes & threads
29
+ class Parallel
30
+ def initialize(&block)
31
+ @channel = Channel.new
32
+ @result_callback = block
33
+ end
34
+
35
+ def work(workers_count = 2, &block)
36
+ spawn_workers(workers_count, &block)
37
+
38
+ while (data = @channel.read)
39
+ @result_callback.call data
40
+ end
41
+ end
42
+
43
+ def spawn_workers(count, &block)
44
+ proc_count, thr_count = Parallel.calc_resources count
45
+ proc_count.times do |i|
46
+ ::Process.fork do
47
+ spawn_threads(thr_count, i, &block).each(&:join)
48
+ end
49
+ end
50
+ end
51
+
52
+ def spawn_threads(count, proc_i, &block)
53
+ count.times.map do |i|
54
+ Thread.new do
55
+ @channel.instance_exec(proc_i, i, &block)
56
+ end
57
+ end
58
+ end
59
+
60
+ class << self
61
+ def calc_resources(count)
62
+ kernels_count = Etc.nprocessors
63
+ proc_count = [count, kernels_count].min
64
+ thr_count = [1, count / proc_count].max
65
+ [proc_count, thr_count]
66
+ end
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multiprocess-threads
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Yudin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: More effective threading using multiple processes and channel
14
+ email: fagci.nsk@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/multiprocess-threads.rb
20
+ homepage: https://rubygems.org/gems/multiprocess-threads
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.3.23
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Multiprocess threads
43
+ test_files: []